From 6923c6b3decc8312c765d672847beb8c8040a5e4 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Wed, 31 May 2023 14:48:51 +0200 Subject: [PATCH] chore: simplify waitWithTimeout (#10278) --- packages/puppeteer-core/src/common/util.ts | 57 +++++++++------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index 406ab5686cb..8c51bbe60da 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -22,14 +22,13 @@ import type {ElementHandle} from '../api/ElementHandle.js'; import type {JSHandle} from '../api/JSHandle.js'; import {Page} from '../api/Page.js'; import {isNode} from '../environment.js'; -import {createDeferred} from '../puppeteer-core.js'; import {assert} from '../util/assert.js'; +import {createDeferred} from '../util/Deferred.js'; import {isErrorLike} from '../util/ErrorLike.js'; import type {CDPSession} from './Connection.js'; import {debug} from './Debug.js'; import {CDPElementHandle} from './ElementHandle.js'; -import {TimeoutError} from './Errors.js'; import type {CommonEventEmitter} from './EventEmitter.js'; import type {ExecutionContext} from './ExecutionContext.js'; import {CDPJSHandle} from './JSHandle.js'; @@ -392,19 +391,23 @@ export async function waitForEvent( deferred.resolve(event); } }); - return Promise.race([deferred.valueOrThrow(), abortPromise]).then( - r => { - removeEventListeners([listener]); - if (isErrorLike(r)) { - throw r; + return Promise.race([deferred.valueOrThrow(), abortPromise]) + .then( + r => { + removeEventListeners([listener]); + if (isErrorLike(r)) { + throw r; + } + return r; + }, + error => { + removeEventListeners([listener]); + throw error; } - return r; - }, - error => { - removeEventListeners([listener]); - throw error; - } - ); + ) + .finally(() => { + deferred.reject(new Error('Cleared')); + }); } /** @@ -506,26 +509,14 @@ export async function waitWithTimeout( taskName: string, timeout: number ): Promise { - let reject: (reason?: Error) => void; - const timeoutError = new TimeoutError( - `waiting for ${taskName} failed: timeout ${timeout}ms exceeded` - ); - const timeoutPromise = new Promise((_, rej) => { - return (reject = rej); + const deferred = createDeferred({ + message: `waiting for ${taskName} failed: timeout ${timeout}ms exceeded`, + timeout, + }); + + return await Promise.race([promise, deferred.valueOrThrow()]).finally(() => { + deferred.reject(new Error('Cleared')); }); - let timeoutTimer = null; - if (timeout) { - timeoutTimer = setTimeout(() => { - return reject(timeoutError); - }, timeout); - } - try { - return await Promise.race([promise, timeoutPromise]); - } finally { - if (timeoutTimer) { - clearTimeout(timeoutTimer); - } - } } /**