From 6a1e8a9ea9eb3dc9e7028ca451176a849a7d1785 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Sat, 28 Mar 2026 21:46:52 -0300 Subject: [PATCH] fix(timeout): adjust promise params --- src/helpers/Timeout.ts | 74 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/helpers/Timeout.ts b/src/helpers/Timeout.ts index cb5fd9b..4acb42c 100644 --- a/src/helpers/Timeout.ts +++ b/src/helpers/Timeout.ts @@ -7,24 +7,23 @@ * file that was distributed with this source code. */ - export class TimeoutBuilder { - private promise: Promise; + private promise: Promise private options: { - ms?: number; - onTimeout?: () => any; - onError?: (error: Error) => any; - }; + ms?: number + onTimeout?: () => any + onError?: (error: Error) => any + } public constructor(promise: Promise) { - this.options = {}; - this.promise = promise; + this.options = {} + this.promise = promise } /** * Define the timeout in milliseconds. - * + * * @example * ```ts * const result = await Timeout.when(promise) @@ -33,15 +32,15 @@ export class TimeoutBuilder { * ``` */ public ms(ms: number) { - this.options.ms = ms; + this.options.ms = ms - return this; + return this } /** * Define the callback to be executed when an error occurs handling * the promise. - * + * * @example * ```ts * const result = await Timeout.when(promise) @@ -51,14 +50,14 @@ export class TimeoutBuilder { * ``` */ public onError(closure: (error: Error) => Promise) { - this.options.onError = closure; + this.options.onError = closure - return this; + return this } /** * Define the callback to be executed when the timeout occurs. - * + * * @example * ```ts * const result = await Timeout.when(promise) @@ -68,14 +67,14 @@ export class TimeoutBuilder { * ``` */ public onTimeout(closure: () => any) { - this.options.onTimeout = closure; + this.options.onTimeout = closure - return this; + return this } /** * Race the promise with timeout. - * + * * @example * ```ts * const result = await Timeout.when(promise) @@ -87,47 +86,48 @@ export class TimeoutBuilder { */ public async race() { if (!this.options.ms) { - throw new Error("ms is required"); + throw new Error('ms is required') } if (!this.options.onTimeout) { - throw new Error("onTimeout is required"); + throw new Error('onTimeout is required') } - let timeoutId: NodeJS.Timeout; + let timeoutId: NodeJS.Timeout + // eslint-disable-next-line promise/param-names const timeout = new Promise((_, reject) => { timeoutId = setTimeout( - () => reject(new Error("__RaceTimeout__")), - this.options.ms, - ); - }); + () => reject(new Error('__RaceTimeout__')), + this.options.ms + ) + }) return Promise.race([this.promise, timeout]) - .then((result) => { - clearTimeout(timeoutId); - return result; + .then(result => { + clearTimeout(timeoutId) + return result }) - .catch((error) => { - clearTimeout(timeoutId); + .catch(error => { + clearTimeout(timeoutId) - if (error.message === "__RaceTimeout__" && this.options.onTimeout) { - return this.options.onTimeout(); + if (error.message === '__RaceTimeout__' && this.options.onTimeout) { + return this.options.onTimeout() } if (this.options.onError) { - return this.options.onError(error); + return this.options.onError(error) } - throw error; - }); + throw error + }) } } export class Timeout { /** * Create a new timeout builder. - * + * * @example * ```ts * const result = await Timeout.when(promise) @@ -138,6 +138,6 @@ export class Timeout { * ``` */ public static when(promise: Promise) { - return new TimeoutBuilder(promise); + return new TimeoutBuilder(promise) } }