just-retry-it
v0.2.1
Published
Retries with error handler
Downloads
814
Readme
just-retry-it
A simple async/await
wrapper around promise-retry and retry.
Why?
- Simplify the interface. You don't need to write the
try-catch-retry
part yourself. - Accept an optional error handler function that will be called after each failed attempt.
Kudos to promise-retry and retry authors 🙏.
Installation
$ npm install just-retry-it
Usage
await retry(operation, options?)
operation
: The operation to be executed and retried if the execution should fail.options
: The options object as it is desrcibed in the retry NPM package.options
also supports theerrorHandler
property (optional) to pass an error handler function, which will be called right after theoperation
if it throws. The handler receives the thrown error as the input.
Example
import retry from 'just-retry-it';
async function getMessage() {
if (Math.random() > 0.5) {
return "Hello World!";
}
throw new Error("boom");
}
async function run() {
// Retry without the error handler.
const msg1 = await retry(getMessage, { retries: 5 });
async function errorHandler(error) {
console.log("received an error", error);
}
// Retry with the error handler.
const msg2 = await retry(getMessage, { retries: 5, errorHandler });
}