Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.