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
npm install p-retry
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.3
Supply Chain
98.4
Quality
82.7
Maintenance
100
Vulnerability
100
License
JavaScript (96.52%)
TypeScript (3.48%)
Total Downloads
3,199,121,920
Last Day
3,465,605
Last Week
17,981,098
Last Month
73,769,619
Last Year
792,917,258
MIT License
866 Stars
72 Commits
71 Forks
8 Watchers
1 Branches
21 Contributors
Updated on May 14, 2025
Minified
Minified + Gzipped
Latest Version
6.2.1
Package Id
p-retry@6.2.1
Unpacked Size
12.93 kB
Size
4.09 kB
File Count
5
NPM Version
10.6.0
Node Version
18.20.4
Published on
Nov 15, 2024
Cumulative downloads
Total Downloads
Last Day
3.2%
3,465,605
Compared to previous day
Last Week
13%
17,981,098
Compared to previous week
Last Month
-0%
73,769,619
Compared to previous month
Last Year
13.6%
792,917,258
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
8 commit(s) and 8 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
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 2025-05-05
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