async-retry
v1.3.3async-retry
Retrying made simple, easy, and async.
Usage
// Packages
const retry = require('async-retry');
const fetch = require('node-fetch');
await retry(
async (bail) => {
// if anything throws, we retry
const res = await fetch('https://google.com');
if (403 === res.status) {
// don't retry upon 403
bail(new Error('Unauthorized'));
return;
}
const data = await res.text();
return data.substr(0, 500);
},
{
retries: 5,
}
);
API
retry(retrier : Function, opts : Object) => Promise
- The supplied function can be
async
or not. In other words, it can be a function that returns aPromise
or a value. - The supplied function receives two parameters
- A
Function
you can invoke to abort the retrying (bail) - A
Number
identifying the attempt. The absolute first attempt (before any retries) is1
.
- A
- The
opts
are passed tonode-retry
. Read its docsretries
: The maximum amount of times to retry the operation. Default is10
.factor
: The exponential factor to use. Default is2
.minTimeout
: The number of milliseconds before starting the first retry. Default is1000
.maxTimeout
: The maximum number of milliseconds between two retries. Default isInfinity
.randomize
: Randomizes the timeouts by multiplying with a factor between1
to2
. Default istrue
.onRetry
: an optionalFunction
that is invoked after a new retry is performed. It's passed theError
that triggered it as a parameter.
Authors
- Guillermo Rauch (@rauchg) - Vercel
- Leo Lamprecht (@notquiteleo) - Vercel
npm i async-retry
Metadata
- MIT
- Whatever
- Unknown
- released 8/17/2021
Downloads
Maintainers
redacted-vercel, gkaragkiaouris, matheuss, igorklopov, leo, nkzawa, tootallnate, rauchg, timneutkens, javivelasco, iamevilrabbit, joecohens, quietshu, dav-is, juancampa, styfle, zeit-bot, lucleray, mglagola, andybitz, paulogdm, anatrajkovska, timer, umegaya, arzafran, ijjk, mfix22, lfades, rabaut, coetry, msweeneydev, williamli, ragojose, guybedford, paco, skllcrn, janicklas-ralph, atcastle, keanulee, spanicker, developit, housseindjirdeh, gmonaco, kikobeats, prateekbh, jkrems, jaredpalmer, gielcobben, chibicode, nazarenooviedo, samsisle, okbel, hankvercel, leerobinson, elsigh, julianbenegas, rizbizkits, raunofreiberg, sokra, cl3arglass, chriswdmr, ernestd, ismaelrumzan, jhoch, mitchellwright, mrmckeb, kuvos, creationix, aboodman, huozhi, cmvnk, arv, ktcarter, aspctub, padmaia, delba, catsaremlg, steventey, gsandhu, dbredvick,