ctry
v1.1.1
Published
ctry tries to resolve a promise with timeout and chance configurations.
Downloads
31
Readme
Intro
ctry
tries to resolve a promise with timeout
and chance
configurations.
/**
* ctry
* @param ctryFunc a function returns a promise
* @param timeout timeout in ms
* @param tries number of changes
*/
function ctry<T>(
ctryFunc: CtryFunction<T>,
timeout: number = 5000,
tries: number = 1,
): Promise<T>;
Q: When shoul you use it? A: Trying to do sth. with timeout, and/or give it more than one chance.
eg. Trying validate a web server whether it is reachable. The codes use HEAD
method to do a request, and try 3 times.
import axios from 'axios';
import { ctry } from 'ctry';
(async () => {
try {
await ctry(() => axios.head('https://example.com'), 5000, 3);
} catch (e) {
console.error(e);
}
})();