diff --git a/packages/puppeteer-core/src/util/Deferred.ts b/packages/puppeteer-core/src/util/Deferred.ts index 47184aa2b43..a512a928728 100644 --- a/packages/puppeteer-core/src/util/Deferred.ts +++ b/packages/puppeteer-core/src/util/Deferred.ts @@ -26,16 +26,15 @@ export class Deferred { this.#resolver = resolve; }); #timeoutId: ReturnType | undefined; - #timeoutError: TimeoutError; + #timeoutError: TimeoutError | undefined; constructor(opts?: DeferredOptions) { - this.#timeoutError = new TimeoutError(opts && opts.message); - this.#timeoutId = - opts && opts.timeout > 0 - ? setTimeout(() => { - this.reject(this.#timeoutError); - }, opts.timeout) - : undefined; + if (opts && opts.timeout > 0) { + this.#timeoutError = new TimeoutError(opts.message); + this.#timeoutId = setTimeout(() => { + this.reject(this.#timeoutError!); + }, opts.timeout); + } } #finish(value: T | V | TimeoutError) {