Skip to content

Instantly share code, notes, and snippets.

@hub-cap
Created June 19, 2014 15:13
Show Gist options
  • Save hub-cap/da568cb3b9d496e9b320 to your computer and use it in GitHub Desktop.
Save hub-cap/da568cb3b9d496e9b320 to your computer and use it in GitHub Desktop.
function retryCall(numTimes, thisArg, callback) {
var theArgs = arguments;
// grab the callback function, assuming (err, data) as the callback
var lastArg = arguments[arguments.length-1];
values = Array.prototype.slice.call(arguments, 3, arguments.length-1);
values.push(function(err, data) {
if (err) {
console.log("an error occurred, but trying again");
console.log(err);
var newNumTimes = theArgs[0] - 1;
if (newNumTimes > 0) {
theArgs[0] = newNumTimes;
retryCall.apply(thisArg, Array.prototype.slice.call(theArgs));
}
} else {
lastArg(null, data);
}
});
// console.log("attempting to call", callback, "with", values);
callback.apply(thisArg, values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment