Created
February 16, 2010 01:31
-
-
Save kriskowal/305189 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
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