diff --git a/src/models/Call.ts b/src/models/Call.ts index 9daca1589c..aafbadb8c8 100644 --- a/src/models/Call.ts +++ b/src/models/Call.ts @@ -70,21 +70,18 @@ const waitForEvent = async ( let listener: (...args: any[]) => void; const wait = new Promise((resolve) => { listener = (...args) => { - if (pred(...args)) { - resolve(); - if (customTimeout === false) { - emitter.off(event, listener!); - } - } + if (pred(...args)) resolve(); }; emitter.on(event, listener); }); if (customTimeout !== false) { const timedOut = (await timeout(wait, false, customTimeout ?? TIMEOUT_MS)) === false; - emitter.off(event, listener!); if (timedOut) throw new Error("Timed out"); + } else { + await wait; } + emitter.off(event, listener!); }; export enum ConnectionState {