Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@pdedg/promise-retry
Advanced tools
Readme
promise-retry A promise retry executor with exponential backoff strategy. Comes with fetch retry out of the box
Install the package:
npm i @pdedg/promise-retry
A promise retry executor
// Simple execution
import { promiseRetry } from '@pdedg/promise-retry';
const run = () => new Promise((resolve, reject) => reject());
promiseRetry(run); // will execute rejected promise by default 3 times and after will trow error
// With custom delay strategy
import { promiseRetry } from '@pdedg/promise-retry';
const options = {
retries: 5,
retryOn: (err) => true,
delay: (err) => err.attempt * 1000,
onFailedAttempt: (err) => console.log(err.status)
}
const run = () => new Promise((resolve, reject) => reject());
promiseRetry(run);
/*
Will result of: 5 executions of failed promise ( retryOn is set to all ways true ).
On each attempt it will execute the onFailedAttempt, and will use linear delay between executions ( last will be 5s delay )
*/
Wraps a fetch request with retry, by default it will retry on the following http status [408, 503, 504] and on request-timeout error.
// Simple execution
import { fetchRetry } from '@pdedg/promise-retry';
const fetch = () => new Promise((resolve, reject) => reject());
fetchRetry(fetch, '/end-point', { delay: 100 }); // Will execute 3 fetch calls with 100 / 200 / 400 delay and eventually an error will be thrown.
// With custom options
import { fetchRetry } from '@pdedg/promise-retry';
const options = {
retries: 2,
retryOnResolved: (res) => {
if (res.status === 300) {
throw new Error('Cannot handle 300 status.')
}
},
}
const fetch = () => new Promise((resolve, reject) => resolve({ status: 300 }));
fetchRetry(fetch, '/end-point', options)
/*
Will result of: 2 executions of resolved promises, that will be rejected by the validate resolved method.
eventually, it will throw an error.
*/
FAQs
Promise with retry executor
The npm package @pdedg/promise-retry receives a total of 12 weekly downloads. As such, @pdedg/promise-retry popularity was classified as not popular.
We found that @pdedg/promise-retry demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.