Skip to content

Instantly share code, notes, and snippets.

@itaysk
Last active August 28, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itaysk/3c3a5c36fae31e464802b61f1239bb2c to your computer and use it in GitHub Desktop.
Save itaysk/3c3a5c36fae31e464802b61f1239bb2c to your computer and use it in GitHub Desktop.
Submit multiple http promised requests with retry
//demonstrates how to submit multiple http requests, while making sure each is resilient to transient errors using retries
var Promise = require('bluebird');
var rp = require('request-promise');
var retry = require('bluebird-retry');
var createPromise = function(i) {
return retry(function () {
return rp('http://localhost:8000?q='+i).promise();
}, {
max_tries: 5,
interval: 1000,
backoff: 2,
predicate: function(err) {
return err.statusCode == 503;
}
}).reflect();
};
var requests = [];
for (var i = 0; i < 3; i++) {
requests.push(createPromise(i));
}
Promise.all(requests).each(function(i) {
if (i.isFulfilled()) {
console.log('Success!');
}
else {
console.log('Fail...');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment