Last active
August 28, 2016 14:07
-
-
Save itaysk/3c3a5c36fae31e464802b61f1239bb2c to your computer and use it in GitHub Desktop.
Submit multiple http promised requests with retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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