chore: create TimeoutError only when needed (#11121)

This commit is contained in:
Nikolay Vitkov 2023-10-11 19:15:06 +02:00 committed by GitHub
parent a3fce3118a
commit e34716ab0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,16 +26,15 @@ export class Deferred<T, V extends Error = Error> {
this.#resolver = resolve; this.#resolver = resolve;
}); });
#timeoutId: ReturnType<typeof setTimeout> | undefined; #timeoutId: ReturnType<typeof setTimeout> | undefined;
#timeoutError: TimeoutError; #timeoutError: TimeoutError | undefined;
constructor(opts?: DeferredOptions) { constructor(opts?: DeferredOptions) {
this.#timeoutError = new TimeoutError(opts && opts.message); if (opts && opts.timeout > 0) {
this.#timeoutId = this.#timeoutError = new TimeoutError(opts.message);
opts && opts.timeout > 0 this.#timeoutId = setTimeout(() => {
? setTimeout(() => { this.reject(this.#timeoutError!);
this.reject(this.#timeoutError); }, opts.timeout);
}, opts.timeout) }
: undefined;
} }
#finish(value: T | V | TimeoutError) { #finish(value: T | V | TimeoutError) {