rtry allows you to easily add promise retry logic to classes, and methods
npm install --save rtry
you can define a universal decorator like this
import rtry from 'rtry';
class DecoratorExample {
@rtry({retries: 10, verbose: true})
static canRetry () {
if (Math.random() < 0.5) {
throw new Error('random error');
}
}
}
DecoratorExample.canRetry();
import rtry from 'rtry';
let functionExample = () => {
const rand = Math.random();
if (rand < 0.5) {
throw new Error('random error');
}
return rand;
};
let functionExampleRetry = rtry({retries: 10, verbose: true}, functionExample);
functionExampleRetry().then(result => {
console.log(result);
});
- beforeRetry (function / async function)
- retries (number)
- delay (number / function / async function)
- verbose (boolean)
you can handle error logging in a custom way as well
const debug = requre('debug')('example');
@rtry({beforeRetry: ({retry, error}) => debug(error.stack)})
class Example {
static canError () {
throw new Error('abcd');
}
}