diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index 217af5039fc..2c9c872eac0 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -1673,7 +1673,7 @@ export class Page extends EventEmitter { return false; }, timeout, - abortDeferred.valueOrThrow() + abortDeferred ); }; @@ -1685,7 +1685,11 @@ export class Page extends EventEmitter { evaluate(); - await Deferred.race([idleDeferred, ...eventPromises, closedDeferred]).then( + // We don't want to reject the closed deferred when + // the race if finished so we pass the Promise instead + const closedPromise = closedDeferred.valueOrThrow(); + + await Deferred.race([idleDeferred, ...eventPromises, closedPromise]).then( r => { cleanup(); return r; diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index a1077a58e6b..5ac0083ae3d 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -383,7 +383,7 @@ export async function waitForEvent( eventName: string | symbol, predicate: (event: T) => Promise | boolean, timeout: number, - abortPromise: Promise + abortPromise: Promise | Deferred ): Promise { const deferred = Deferred.create({ message: `Timeout exceeded while waiting for event ${String(eventName)}`, diff --git a/packages/puppeteer-core/src/util/Deferred.ts b/packages/puppeteer-core/src/util/Deferred.ts index 424b7433775..7ddbc000df7 100644 --- a/packages/puppeteer-core/src/util/Deferred.ts +++ b/packages/puppeteer-core/src/util/Deferred.ts @@ -89,7 +89,9 @@ export class Deferred { try { const promises = awaitables.map(value => { if (value instanceof Deferred) { - deferredWithTimeout.add(value); + if (value.#timeoutId) { + deferredWithTimeout.add(value); + } return value.valueOrThrow(); }