Skip to content

Commit d66a77a

Browse files
committed
Updated README for usage with promises
1 parent 2cd5c6a commit d66a77a

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOU
66

77
## Usage
88

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
1012

1113
```javascript
1214
var request = require('requestretry');
@@ -15,7 +17,7 @@ request({
1517
url: 'https://api.domain.com/v1/a/b'
1618
json:true,
1719

18-
// The above parameters are specific to Request-retry
20+
// The below parameters are specific to Request-retry
1921
maxAttempts: 5, // (default) try 5 times
2022
retryDelay: 5000, // (default) wait for 5s before trying again
2123
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
@@ -27,6 +29,61 @@ request({
2729
});
2830
```
2931

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+
function customPromiseFactory(resolver) {
63+
// With when.js
64+
return require('when').promise(resolver);
65+
66+
// With RSVP.js
67+
var Promise = require('rsvp').Promise;
68+
69+
return new Promise(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+
3087
## Installation
3188

3289
Install with [npm](https://npmjs.org/package/requestretry).

0 commit comments

Comments
 (0)