You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+59-2Lines changed: 59 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,9 @@ When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOU
6
6
7
7
## Usage
8
8
9
-
Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`. It also adds one property to the response, `attempts`.
9
+
Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`. It also adds one property to the response, `attempts`. It supports callbacks or promises.
10
+
11
+
### With callbacks
10
12
11
13
```javascript
12
14
var request =require('requestretry');
@@ -15,7 +17,7 @@ request({
15
17
url:'https://api.domain.com/v1/a/b'
16
18
json:true,
17
19
18
-
// The above parameters are specific to Request-retry
20
+
// The below parameters are specific to Request-retry
19
21
maxAttempts:5, // (default) try 5 times
20
22
retryDelay:5000, // (default) wait for 5s before trying again
21
23
retryStrategy:request.RetryStrategies.HTTPOrNetworkError// (default) retry on 5xx or network errors
@@ -27,6 +29,61 @@ request({
27
29
});
28
30
```
29
31
32
+
### With promises
33
+
34
+
When you're using promises, you can pass the two following options:
35
+
-`fullResponse`_(default true)_ - To resolve the promise with the full response or just the body
36
+
-`promiseFactory`_(default bluebird)_ - A function to allow the usage of a different promise implementation library
37
+
38
+
```javascript
39
+
request({
40
+
url:'https://api.domain.com/v1/a/b'
41
+
json:true,
42
+
43
+
fullResponse:true// (default) To resolve the promise with the full response or just the body
44
+
})
45
+
.then(function (response) {
46
+
// response = The full response object or just the body
47
+
})
48
+
.catch(function(error) {
49
+
// error = Any occurred error
50
+
})
51
+
```
52
+
53
+
**Using `promiseFactory` option to use a different promise implementation library**
54
+
55
+
```javascript
56
+
// See the tests for different libraries usage examples
57
+
58
+
/**
59
+
* @param{Function}resolver The promise resolver function
60
+
* @return{Object} The promise instance
61
+
*/
62
+
functioncustomPromiseFactory(resolver) {
63
+
// With when.js
64
+
returnrequire('when').promise(resolver);
65
+
66
+
// With RSVP.js
67
+
varPromise=require('rsvp').Promise;
68
+
69
+
returnnewPromise(resolver);
70
+
}
71
+
72
+
request({
73
+
url:'https://api.domain.com/v1/a/b'
74
+
json:true,
75
+
76
+
// Custom promise factory function
77
+
promiseFactory: customPromiseFactory
78
+
})
79
+
.then(function (response) {
80
+
// response = The full response object or just the body
81
+
})
82
+
.catch(function(error) {
83
+
// error = Any occurred error
84
+
})
85
+
```
86
+
30
87
## Installation
31
88
32
89
Install with [npm](https://npmjs.org/package/requestretry).
0 commit comments