async-retry-wrapper
Documentation
Retry wrapper for async function.
Installation
$ npm install async-retry-wrapper
Javascript
const retryWrapper = require('async-retry-wrapper');
Typescript
import * as retryWrapper from 'async-retry-wrapper';
Usage
functions
An object consisting of an async function may be used.
If it is not an async function, an error occurs.
const retryWrapper = require('async-retry-wrapper');
const functions = {
logger: async () => {
console.log('hello world!!');
},
};
const wrappedFunctions = retryWrapper(functions);
option
You can also change the retry settings if you want(If there is no parameter, it operates as Default).
There are a total of three options.
- options
- count : How many times are you going to try again?
- interval: How often will you try again?, 0 means as quickly as possible.
- default: 0 (milliseconds)
- rule: When are you gonna stop trying again?, Only one parameter is error.
- default: Retry if an error occurs
The example is an option to retry twice more at 100 ms intervals when the response status is not 500.
const retryWrapper = require('async-retry-wrapper');
const options = {
count: 2,
interval: 100,
rule: err => err.status !== 500,
};
const wrappedFunctions = retryWrapper(someFunObject, options);
Demo
const retryWrapper = require('async-retry-wrapper');
const testFunctions = {
logAndThrow: async () => {
console.log('welcome');
throw new Error('with occur error!');
},
log: async () => {
console.log('hello world!');
},
};
const wrappedFunctions = retryWrapper(testFunctions);
wrappedFunctions['logAndThrow']();
License
MIT License