Предложу такой вариант...
class DummyClient {
connect() {
return new Promise(resolve => {
setTimeout(() => {
console.log('connect');
resolve()
}
, 2000);
})
}
async doSomething() {
console.log('done something')
}
}
class ApiWrapper {
#client;
#connect = false
async getClient() {
this.connect = true
const client = new DummyClient();
await client.connect();
this.#client = client;
}
async doSomething(n) {
while (!this.#client) {
if (!this.connect) await this.getClient();
await this.pause(100)
}
const v = await this.#client.doSomething();
return v
}
pause(t) {
return new Promise(resolve => {
setTimeout(resolve, t);
})
}
}
const run = async()=>{
const api = new ApiWrapper();
await Promise.all([
api.doSomething(1),
api.doSomething(2),
api.doSomething(3),
api.doSomething(4)
]);
}
run();