ts-retry
A little retry tool in javascript/typescript for node and for browser. Can also bind a timeout to a function.
How to:
- to retry something:
const result = await retry(()=> {}, { delay:100, maxTry:5 );
- to retry something async :
const result = await retryAsync(async ()=> {}, { delay:100, maxTry:5 );
- Need to call a function at multiple place with same retryOptions ? Use decorators:
const fn = (title: string, count:number) => return `${count}. ${title}`;
const decoratedFn = retryDecorator(fn, { delay:100, maxTry:5 });
const title1 = await decoratedFn("Intro", 1);
const title2 = await decoratedFn("A chapter", 2);
const fn = async (name: string): Promise<any> => { };
const decoratedFn = retryAsyncDecorator(fn, {delay:100,maxTry:5});
const result1 = await decoratedFn("John");
const result2 = await decoratedFn("Doe");
- to wait:
await wait(10000);
- to set a timeout:
try {
const result = await waitUntil(async ()=> {}, 10000);
} catch (err) {
if (isTimeoutError(error)) { {
} else {
}
}
- to set a timeout on something async:
try {
const result = await waitUntilAsync(async ()=> {}, 10000);
} catch (err) {
if (isTimeoutError(error)) {
} else {
}
}
- Need to call a function at multiple place with same durations ? Use decorators:
const fn = (title: string, count:number) => ;
const decoratedFn = waitUntilDecorator(fn, { delay:100, maxTry:5 });
const title1 = await decoratedFn("Intro", 1);
const title2 = await decoratedFn("A chapter", 2);
const fn = async (name: string): Promise<any> => { };
const decoratedFn = waitUntilAsyncDecorator(fn, {delay:100,maxTry:5});
const result1 = await decoratedFn("John");
const result2 = await decoratedFn("Doe");
API
Retry familly
Wait familly
wait(duration?)
: Do nothing during "duration" millisecondswaitUntil<T>(fn<T>, duration?, error?)
: waitUntil call asynchronously fn once. If fn complete within the duration (express in miliseconds), waitUntil returns the fn result. Otherwhise it thows the given error (if any) or a TimeoutError exception.waitUntilAsync<T>(fn<T>, duration?, error?)
: same as waitUntil, except fn is an asynchronous function.TimeoutError
: an error thrown by waitUntil and waitUntilAsync. It comes with a isTimeoutError type guard:
if (isTimeoutError(error)) {
}
In case of timeout fn is still executing. It is advise to add a mean to abort it.
- When duration is not provided, the default one is applyed. The default default is 60000ms.
setDefaultDuration(duration: number)
: change the default duration.getDefaultDuration()
: returns the current default duration.waitUntilAsyncDecorator(fn: T, duration?: number, error?: Error)
and waitUntilDecorator(fn: T, duration?: number, error?: Error)
: decorators that return a function with same signature than the given function. On decorated call, fn is called bounded to the duration.
Compatilibity
This lib works with Deno (to import it,use the url https://raw.githubusercontent.com/franckLdx/ts-retry/<version>/src/index.ts
). However it's more convenient to use the specific port of this lib to Deno: see https://deno.land/x/retry