Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Created April 15, 2017 00:14
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 oieduardorabelo/a7ea9bd0c445e31a79811e1c12d040ab to your computer and use it in GitHub Desktop.
Save oieduardorabelo/a7ea9bd0c445e31a79811e1c12d040ab to your computer and use it in GitHub Desktop.
const superagent = require('superagent');
const NUM_RETRIES = 3;
request('http://google.com/this-throws-an-error', function(error, res) {
console.log(error.message); // "Not Found"
});
function request(url, callback) {
_request(url, 0, callback);
}
function _request(url, retriedCount, callback) {
superagent.get(url).end(function(error, res) {
if (error) {
if (retriedCount >= NUM_RETRIES) {
return callback && callback(error);
}
return _request(url, retriedCount + 1, callback);
}
callback(res);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment