From 138cc5c961da698bf7ca635c9947058df4b2ec72 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Mon, 19 Jun 2023 10:11:59 +0200 Subject: [PATCH] fix: WaitForNetworkIdle and Deferred.race (#10411) --- packages/puppeteer-core/src/api/Page.ts | 8 ++++++-- packages/puppeteer-core/src/common/util.ts | 2 +- packages/puppeteer-core/src/util/Deferred.ts | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) 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(); }