Gathering detailed insights and metrics for p-retry
Gathering detailed insights and metrics for p-retry
Gathering detailed insights and metrics for p-retry
Gathering detailed insights and metrics for p-retry
better-retry
Basically a rewrite of p-retry
@common.js/p-retry
p-retry package exported as CommonJS modules
@n8n/p-retry
Retry a promise-returning or async function
@smithy/middleware-retry
[![NPM version](https://img.shields.io/npm/v/@smithy/middleware-retry/latest.svg)](https://www.npmjs.com/package/@smithy/middleware-retry) [![NPM downloads](https://img.shields.io/npm/dm/@smithy/middleware-retry.svg)](https://www.npmjs.com/package/@smithy
npm install p-retry
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
799 Stars
64 Commits
66 Forks
7 Watching
2 Branches
21 Contributors
Updated on 26 Nov 2024
JavaScript (92.42%)
TypeScript (7.58%)
Cumulative downloads
Total Downloads
Last day
-2.1%
3,193,991
Compared to previous day
Last week
3.1%
16,804,935
Compared to previous week
Last month
13.4%
68,823,759
Compared to previous month
Last year
5.1%
733,998,360
Compared to previous year
3
Retry a promise-returning or async function
It does exponential backoff and supports custom retry strategies for failed operations.
1npm install p-retry
1import pRetry, {AbortError} from 'p-retry'; 2import fetch from 'node-fetch'; 3 4const run = async () => { 5 const response = await fetch('https://sindresorhus.com/unicorn'); 6 7 // Abort retrying if the resource doesn't exist 8 if (response.status === 404) { 9 throw new AbortError(response.statusText); 10 } 11 12 return response.blob(); 13}; 14 15console.log(await pRetry(run, {retries: 5}));
Returns a Promise
that is fulfilled when calling input
returns a fulfilled promise. If calling input
returns a rejected promise, input
is called again until the maximum number of retries is reached. It then rejects with the last rejection reason.
It does not retry on most TypeError
's, with the exception of network errors. This is done on a best case basis as different browsers have different messages to indicate this. See whatwg/fetch#526 (comment)
Type: Function
Receives the current attempt number as the first argument and is expected to return a Promise
or any value.
Type: object
Options are passed to the retry
module.
Type: Function
Callback invoked on each retry. Receives the error thrown by input
as the first argument with properties attemptNumber
and retriesLeft
which indicate the current attempt number and the number of attempts left, respectively.
1import pRetry from 'p-retry'; 2 3const run = async () => { 4 const response = await fetch('https://sindresorhus.com/unicorn'); 5 6 if (!response.ok) { 7 throw new Error(response.statusText); 8 } 9 10 return response.json(); 11}; 12 13const result = await pRetry(run, { 14 onFailedAttempt: error => { 15 console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`); 16 // 1st request => Attempt 1 failed. There are 4 retries left. 17 // 2nd request => Attempt 2 failed. There are 3 retries left. 18 // … 19 }, 20 retries: 5 21}); 22 23console.log(result);
The onFailedAttempt
function can return a promise. For example, you can do some async logging:
1import pRetry from 'p-retry'; 2import logger from './some-logger'; 3 4const run = async () => { … }; 5 6const result = await pRetry(run, { 7 onFailedAttempt: async error => { 8 await logger.log(error); 9 } 10});
If the onFailedAttempt
function throws, all retries will be aborted and the original promise will reject with the thrown error.
Type: Function
Decide if a retry should occur based on the error. Returning true triggers a retry, false aborts with the error.
It is not called for TypeError
(except network errors) and AbortError
.
1import pRetry from 'p-retry'; 2 3const run = async () => { … }; 4 5const result = await pRetry(run, { 6 shouldRetry: error => !(error instanceof CustomError); 7});
In the example above, the operation will be retried unless the error is an instance of CustomError
.
Type: AbortSignal
You can abort retrying using AbortController
.
1import pRetry from 'p-retry'; 2 3const run = async () => { … }; 4const controller = new AbortController(); 5 6cancelButton.addEventListener('click', () => { 7 controller.abort(new Error('User clicked cancel button')); 8}); 9 10try { 11 await pRetry(run, {signal: controller.signal}); 12} catch (error) { 13 console.log(error.message); 14 //=> 'User clicked cancel button' 15}
Abort retrying and reject the promise.
Type: string
An error message.
Type: Error
A custom error.
You can pass arguments to the function being retried by wrapping it in an inline arrow function:
1import pRetry from 'p-retry'; 2 3const run = async emoji => { 4 // … 5}; 6 7// Without arguments 8await pRetry(run, {retries: 5}); 9 10// With arguments 11await pRetry(() => run('🦄'), {retries: 5});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
2 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More