Add retry logic to a Promise-returning function.
Install using npm:
$ npm install retriable-promise
retriable-promise exports a function that accepts a Promise-returning function and an options object, returning a new function that decorates the given one with retry logic:
retriable(func, options)
const retriable = require('retriable-promise');
const api = require('./my-api-module');
const fetchStuff = retriable(api.fetchStuff, {
// Retry 3 times, delayed 0/500/1000 ms respectively
retries: [0, 500, 1000],
// Only when the status code is 429
when(err) {
return err.statusCode === 429;
}
});
An array of integers, representing the delay (in ms) to wait before invoking
each retry attempt. For example, passing { wait: [1000, 1000, 1000] }
would
configure the returning function to retry up to 3 times, waiting 1 second
before each subsequent attempt.
By default, retries will be invoked for all Promise rejections. In order to
refine which failures are retriable, specify a when
function that receives
the Promise rejection value as an argument and returns true
or false
depending on whether a retry should be made.
The particular Promise implementation can be overridden by specifying
options.Promise
, which defaults to the native ES2015 Promise
. Note that if
this option is specified, the given function should conform to the native
Promise constructor API, i.e., it is expected to take a callback function that
itself receives resolve
and reject
callbacks and returns an appropriate
promise instance.
MIT