Skip to content

Instantly share code, notes, and snippets.

@rpl
Forked from kriskowal/promise-error-recovery.js
Created February 16, 2010 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpl/305229 to your computer and use it in GitHub Desktop.
Save rpl/305229 to your computer and use it in GitHub Desktop.
// recovery
var httpReadOrRetry = function (url, timeout, times) {
return http.read(url).then(function (content) {
return content;
}, function (error) {
if (times == 0)
return error;
return delay(timeout).then(function () {
return httpReadOrRetry(url, timeout, times - 1);
});
});
}
// promise-based wrapper for setTimeout
var delay = function (timeout) {
var deferred = new 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