Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions src/helpers/Timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
* file that was distributed with this source code.
*/


export class TimeoutBuilder<T> {
private promise: Promise<T>;
private promise: Promise<T>

private options: {
ms?: number;
onTimeout?: () => any;
onError?: (error: Error) => any;
};
ms?: number
onTimeout?: () => any
onError?: (error: Error) => any
}

public constructor(promise: Promise<T>) {
this.options = {};
this.promise = promise;
this.options = {}
this.promise = promise
}

/**
* Define the timeout in milliseconds.
*
*
* @example
* ```ts
* const result = await Timeout.when(promise)
Expand All @@ -33,15 +32,15 @@ export class TimeoutBuilder<T> {
* ```
*/
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)
Expand All @@ -51,14 +50,14 @@ export class TimeoutBuilder<T> {
* ```
*/
public onError(closure: (error: Error) => Promise<T>) {
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)
Expand All @@ -68,14 +67,14 @@ export class TimeoutBuilder<T> {
* ```
*/
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)
Expand All @@ -87,47 +86,48 @@ export class TimeoutBuilder<T> {
*/
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<never>((_, 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)
Expand All @@ -138,6 +138,6 @@ export class Timeout {
* ```
*/
public static when<T>(promise: Promise<T>) {
return new TimeoutBuilder(promise);
return new TimeoutBuilder(promise)
}
}
Loading