Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
async-retry
Advanced tools
The async-retry npm package is designed to handle the execution of asynchronous operations that may fail by retrying them a specified number of times with customizable options. It is useful for dealing with operations like network requests or any other tasks that might intermittently fail due to temporary issues.
Basic retry functionality
This feature allows you to automatically retry an asynchronous operation if it fails. The code sample shows how to use async-retry to fetch data from an API with up to 5 retries.
const retry = require('async-retry');
async function fetchData() {
return retry(async bail => {
// if anything throws, we retry
const result = await fetch('https://api.example.com/data');
if (result.status >= 400) {
throw new Error('Request failed');
}
return result.json();
}, {
retries: 5
});
}
Custom retry strategies
This feature allows you to define a custom retry strategy, including the number of retries, the factor by which the timeout increases, and the minimum and maximum timeout values. The code sample demonstrates setting these options for a more sophisticated retry strategy.
const retry = require('async-retry');
async function fetchDataWithCustomStrategy() {
return retry(async bail => {
// if anything throws, we retry
const result = await fetch('https://api.example.com/data');
if (result.status >= 400) {
throw new Error('Request failed');
}
return result.json();
}, {
retries: 5,
factor: 2,
minTimeout: 1000,
maxTimeout: 5000,
randomize: true
});
}
Bailing out of retries
This feature allows you to bail out of the retry loop early under certain conditions. The code sample shows how to stop retrying if a 404 status is encountered or after a certain number of attempts.
const retry = require('async-retry');
async function fetchDataWithBail() {
return retry(async (bail, attempt) => {
try {
const result = await fetch('https://api.example.com/data');
if (result.status === 404) {
// Don't retry upon 404
bail(new Error('Not found'));
return;
}
return result.json();
} catch (error) {
if (attempt >= 3) {
// Bail on the third attempt
bail(error);
return;
}
throw error;
}
}, {
retries: 5
});
}
The 'retry' package provides similar functionality for retrying operations, with a focus on both synchronous and asynchronous control flow. It offers a more extensive set of options for customizing retry behavior but does not provide an async/await interface out of the box.
The 'p-retry' package is another alternative that supports retrying asynchronous operations using Promises. It has a similar API to async-retry but is built specifically for use with Promises and provides slightly different options for customizing the retry behavior.
The 'retry-axios' package is designed to work as an interceptor for axios, a popular HTTP client. It allows you to automatically retry failed HTTP requests, and it is specifically tailored for axios users, unlike async-retry which is more generic and can be used with any asynchronous operation.
Retrying made simple, easy, async.
import retry from 'async-retry'
import fetch from '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: 500
})
retry(retrier : Function, opts : Object) => Promise
default
. Use import retry from 'async-retry'
(ES6) or require('async-retry').default
(legacy).async
or not. In other words, it can be a function that returns a Promise
or a value.Function
you can invoke to abort the retrying (bail)Number
identifying the attempt. The absolute first attempt (before any retries) is 1
.opts
are passed to node-retry
. Read its docs
retries
: The maximum amount of times to retry the operation. Default is 10
.factor
: The exponential factor to use. Default is 2
.minTimeout
: The number of milliseconds before starting the first retry. Default is 1000
.maxTimeout
: The maximum number of milliseconds between two retries. Default is Infinity
.randomize
: Randomizes the timeouts by multiplying with a factor between 1
to 2
. Default is false
.onRetry
: an optional Function
that is invoked after a new retry is performed. It's passed the Error
that triggered it as a parameter.FAQs
Retrying made simple, easy and async
We found that async-retry demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 80 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.