Created
April 15, 2017 00:14
-
-
Save oieduardorabelo/a7ea9bd0c445e31a79811e1c12d040ab to your computer and use it in GitHub Desktop.
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
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