Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created February 16, 2010 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kriskowal/305189 to your computer and use it in GitHub Desktop.
Save kriskowal/305189 to your computer and use it in GitHub Desktop.
var HTTP = require("http-client");
var Q = require("promise");
// recovery
var httpReadRetryLoop = function (url, timeout, times) {
return Q.when(HTTP.read(url), function (content) {
return content;
}, function (error) {
if (times == 0)
return error;
return Q.when(delay(timeout), function () {
return httpReadRetryLoop(url, timeout, times - 1);
});
});
}
// promise-based wrapper for setTimeout
var delay = function (timeout) {
var deferred = Q.Deferred();
setTimeout(function () {
deferred.resolve();
}, timeout);
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment