| 1 | # p-retry
|
|---|
| 2 |
|
|---|
| 3 | > Retry a promise-returning or async function
|
|---|
| 4 |
|
|---|
| 5 | It does exponential backoff and supports custom retry strategies for failed operations.
|
|---|
| 6 |
|
|---|
| 7 | ## Install
|
|---|
| 8 |
|
|---|
| 9 | ```
|
|---|
| 10 | $ npm install p-retry
|
|---|
| 11 | ```
|
|---|
| 12 |
|
|---|
| 13 | ## Usage
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | const pRetry = require('p-retry');
|
|---|
| 17 | const fetch = require('node-fetch');
|
|---|
| 18 |
|
|---|
| 19 | const run = async () => {
|
|---|
| 20 | const response = await fetch('https://sindresorhus.com/unicorn');
|
|---|
| 21 |
|
|---|
| 22 | // Abort retrying if the resource doesn't exist
|
|---|
| 23 | if (response.status === 404) {
|
|---|
| 24 | throw new pRetry.AbortError(response.statusText);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | return response.blob();
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | (async () => {
|
|---|
| 31 | console.log(await pRetry(run, {retries: 5}));
|
|---|
| 32 | })();
|
|---|
| 33 | ```
|
|---|
| 34 |
|
|---|
| 35 | ## API
|
|---|
| 36 |
|
|---|
| 37 | ### pRetry(input, options?)
|
|---|
| 38 |
|
|---|
| 39 | 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.
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | #### input
|
|---|
| 46 |
|
|---|
| 47 | Type: `Function`
|
|---|
| 48 |
|
|---|
| 49 | Receives the current attempt number as the first argument and is expected to return a `Promise` or any value.
|
|---|
| 50 |
|
|---|
| 51 | #### options
|
|---|
| 52 |
|
|---|
| 53 | Type: `object`
|
|---|
| 54 |
|
|---|
| 55 | Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module.
|
|---|
| 56 |
|
|---|
| 57 | ##### onFailedAttempt(error)
|
|---|
| 58 |
|
|---|
| 59 | Type: `Function`
|
|---|
| 60 |
|
|---|
| 61 | 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.
|
|---|
| 62 |
|
|---|
| 63 | ```js
|
|---|
| 64 | const run = async () => {
|
|---|
| 65 | const response = await fetch('https://sindresorhus.com/unicorn');
|
|---|
| 66 |
|
|---|
| 67 | if (!response.ok) {
|
|---|
| 68 | throw new Error(response.statusText);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return response.json();
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | (async () => {
|
|---|
| 75 | const result = await pRetry(run, {
|
|---|
| 76 | onFailedAttempt: error => {
|
|---|
| 77 | console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`);
|
|---|
| 78 | // 1st request => Attempt 1 failed. There are 4 retries left.
|
|---|
| 79 | // 2nd request => Attempt 2 failed. There are 3 retries left.
|
|---|
| 80 | // …
|
|---|
| 81 | },
|
|---|
| 82 | retries: 5
|
|---|
| 83 | });
|
|---|
| 84 |
|
|---|
| 85 | console.log(result);
|
|---|
| 86 | })();
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | The `onFailedAttempt` function can return a promise. For example, you can do some async logging:
|
|---|
| 90 |
|
|---|
| 91 | ```js
|
|---|
| 92 | const pRetry = require('p-retry');
|
|---|
| 93 | const logger = require('./some-logger');
|
|---|
| 94 |
|
|---|
| 95 | const run = async () => { … };
|
|---|
| 96 |
|
|---|
| 97 | (async () => {
|
|---|
| 98 | const result = await pRetry(run, {
|
|---|
| 99 | onFailedAttempt: async error => {
|
|---|
| 100 | await logger.log(error);
|
|---|
| 101 | }
|
|---|
| 102 | });
|
|---|
| 103 | })();
|
|---|
| 104 | ```
|
|---|
| 105 |
|
|---|
| 106 | If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error.
|
|---|
| 107 |
|
|---|
| 108 | ### pRetry.AbortError(message)
|
|---|
| 109 | ### pRetry.AbortError(error)
|
|---|
| 110 |
|
|---|
| 111 | Abort retrying and reject the promise.
|
|---|
| 112 |
|
|---|
| 113 | ### message
|
|---|
| 114 |
|
|---|
| 115 | Type: `string`
|
|---|
| 116 |
|
|---|
| 117 | Error message.
|
|---|
| 118 |
|
|---|
| 119 | ### error
|
|---|
| 120 |
|
|---|
| 121 | Type: `Error`
|
|---|
| 122 |
|
|---|
| 123 | Custom error.
|
|---|
| 124 |
|
|---|
| 125 | ## Tip
|
|---|
| 126 |
|
|---|
| 127 | You can pass arguments to the function being retried by wrapping it in an inline arrow function:
|
|---|
| 128 |
|
|---|
| 129 | ```js
|
|---|
| 130 | const pRetry = require('p-retry');
|
|---|
| 131 |
|
|---|
| 132 | const run = async emoji => {
|
|---|
| 133 | // …
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | (async () => {
|
|---|
| 137 | // Without arguments
|
|---|
| 138 | await pRetry(run, {retries: 5});
|
|---|
| 139 |
|
|---|
| 140 | // With arguments
|
|---|
| 141 | await pRetry(() => run('🦄'), {retries: 5});
|
|---|
| 142 | })();
|
|---|
| 143 | ```
|
|---|
| 144 |
|
|---|
| 145 | ## Related
|
|---|
| 146 |
|
|---|
| 147 | - [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time
|
|---|
| 148 | - [More…](https://github.com/sindresorhus/promise-fun)
|
|---|