Created
June 19, 2014 15:13
-
-
Save hub-cap/da568cb3b9d496e9b320 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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