Skip to content

Commit

Permalink
Updated README for usage with promises
Browse files Browse the repository at this point in the history
  • Loading branch information
milsosa committed Dec 24, 2015
1 parent 2cd5c6a commit d66a77a
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOU

## Usage

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`.
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.

### With callbacks

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

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

### With promises

When you're using promises, you can pass the two following options:
- `fullResponse` _(default true)_ - To resolve the promise with the full response or just the body
- `promiseFactory` _(default bluebird)_ - A function to allow the usage of a different promise implementation library

```javascript
request({
url: 'https://api.domain.com/v1/a/b'
json:true,

fullResponse: true // (default) To resolve the promise with the full response or just the body
})
.then(function (response) {
// response = The full response object or just the body
})
.catch(function(error) {
// error = Any occurred error
})
```

**Using `promiseFactory` option to use a different promise implementation library**

```javascript
// See the tests for different libraries usage examples

/**
* @param {Function} resolver The promise resolver function
* @return {Object} The promise instance
*/
function customPromiseFactory(resolver) {
// With when.js
return require('when').promise(resolver);

// With RSVP.js
var Promise = require('rsvp').Promise;

return new Promise(resolver);
}

request({
url: 'https://api.domain.com/v1/a/b'
json:true,

// Custom promise factory function
promiseFactory: customPromiseFactory
})
.then(function (response) {
// response = The full response object or just the body
})
.catch(function(error) {
// error = Any occurred error
})
```

## Installation

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

0 comments on commit d66a77a

Please sign in to comment.