source: frontend/node_modules/p-retry/readme.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.5 KB
Line 
1# p-retry
2
3> Retry a promise-returning or async function
4
5It 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
16const pRetry = require('p-retry');
17const fetch = require('node-fetch');
18
19const 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
39Returns 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
42Does 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
47Type: `Function`
48
49Receives the current attempt number as the first argument and is expected to return a `Promise` or any value.
50
51#### options
52
53Type: `object`
54
55Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module.
56
57##### onFailedAttempt(error)
58
59Type: `Function`
60
61Callback 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
64const 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
89The `onFailedAttempt` function can return a promise. For example, you can do some async logging:
90
91```js
92const pRetry = require('p-retry');
93const logger = require('./some-logger');
94
95const 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
106If 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
111Abort retrying and reject the promise.
112
113### message
114
115Type: `string`
116
117Error message.
118
119### error
120
121Type: `Error`
122
123Custom error.
124
125## Tip
126
127You can pass arguments to the function being retried by wrapping it in an inline arrow function:
128
129```js
130const pRetry = require('p-retry');
131
132const 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)
Note: See TracBrowser for help on using the repository browser.