1
1
# async-retry
2
2
3
- [ ![ Code Style] ( https://badgen.net/badge/code%20style/airbnb/fd5c63 )] ( https://github.com/airbnb/javascript )
4
- [ ![ Join the community on Spectrum] ( https://withspectrum.github.io/badge/badge.svg )] ( https://spectrum.chat/zeit )
5
-
6
3
Retrying made simple, easy, and async.
7
4
8
5
## Usage
9
6
10
7
``` js
11
8
// Packages
12
- const retry = require (' async-retry' )
13
- const fetch = require (' node-fetch' )
14
-
15
- await retry (async bail => {
16
- // if anything throws, we retry
17
- const res = await fetch (' https://google.com' )
18
-
19
- if (403 === res .status ) {
20
- // don't retry upon 403
21
- bail (new Error (' Unauthorized' ))
22
- return
9
+ const retry = require (' async-retry' );
10
+ const fetch = require (' node-fetch' );
11
+
12
+ await retry (
13
+ async (bail ) => {
14
+ // if anything throws, we retry
15
+ const res = await fetch (' https://google.com' );
16
+
17
+ if (403 === res .status ) {
18
+ // don't retry upon 403
19
+ bail (new Error (' Unauthorized' ));
20
+ return ;
21
+ }
22
+
23
+ const data = await res .text ();
24
+ return data .substr (0 , 500 );
25
+ },
26
+ {
27
+ retries: 5 ,
23
28
}
24
-
25
- const data = await res .text ()
26
- return data .substr (0 , 500 )
27
- }, {
28
- retries: 5
29
- })
29
+ );
30
30
```
31
31
32
32
### API
@@ -40,14 +40,14 @@ retry(retrier : Function, opts : Object) => Promise
40
40
1. A ` Function ` you can invoke to abort the retrying (bail)
41
41
2. A ` Number ` identifying the attempt. The absolute first attempt (before any retries) is ` 1 ` .
42
42
- The ` opts` are passed to ` node- retry` . Read [its docs](https://github.com/tim-kos/node-retry)
43
- * ` retries` : The maximum amount of times to retry the operation. Default is ` 10 ` .
44
- * ` factor` : The exponential factor to use. Default is ` 2 ` .
45
- * ` minTimeout` : The number of milliseconds before starting the first retry. Default is ` 1000 ` .
46
- * ` maxTimeout` : The maximum number of milliseconds between two retries. Default is ` Infinity ` .
47
- * ` randomize` : Randomizes the timeouts by multiplying with a factor between ` 1 ` to ` 2 ` . Default is ` true ` .
48
- * ` onRetry` : an optional ` Function ` that is invoked after a new retry is performed. It's passed the ` Error ` that triggered it as a parameter.
43
+ - ` retries` : The maximum amount of times to retry the operation. Default is ` 10 ` .
44
+ - ` factor` : The exponential factor to use. Default is ` 2 ` .
45
+ - ` minTimeout` : The number of milliseconds before starting the first retry. Default is ` 1000 ` .
46
+ - ` maxTimeout` : The maximum number of milliseconds between two retries. Default is ` Infinity ` .
47
+ - ` randomize` : Randomizes the timeouts by multiplying with a factor between ` 1 ` to ` 2 ` . Default is ` true ` .
48
+ - ` onRetry` : an optional ` Function ` that is invoked after a new retry is performed. It's passed the ` Error ` that triggered it as a parameter.
49
49
50
50
## Authors
51
51
52
- - Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [ZEIT ](https://zeit.co )
53
- - Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [ZEIT ](https://zeit.co )
52
+ - Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [Vercel ](https://vercel.com )
53
+ - Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [Vercel ](https://vercel.com )
0 commit comments