Last active
November 24, 2015 21:55
-
-
Save teyc/e842bea34a72fd676602 to your computer and use it in GitHub Desktop.
Doing retries with Promises
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 retry(maxAttempts, millisecond, fn) { | |
| var deferred = $.Deferred(); | |
| var attempts = 0; | |
| function wait(milliseconds) { | |
| var deferred = $.Deferred(); | |
| setTimeout(deferred.resolve, milliseconds); | |
| return deferred.promise(); | |
| } | |
| function execute(fn) { | |
| attempts++; | |
| fn().then(function (successResult) { | |
| deferred.resolve(successResult); | |
| }, function (failResult) { | |
| if (attempts >= maxAttempts) { | |
| deferred.reject(failResult); | |
| return; | |
| } | |
| wait(millisecond) | |
| .then(function () { | |
| execute(fn); | |
| }); | |
| }); | |
| } | |
| execute(fn); | |
| return deferred.promise(); | |
| } |
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 retry(maxAttempts, millisecond, fn) | |
| { | |
| var deferred = Promise.defer(); | |
| var attempts = 0; | |
| function execute(fn) { | |
| attempts++; | |
| fn().then(function (successResult) { | |
| deferred.resolve(successResult); | |
| }, function (failResult) { | |
| if (attempts >= maxAttempts ) { | |
| deferred.reject(failResult); | |
| return; | |
| } | |
| wait(millisecond) | |
| .then(function () { | |
| execute(fn); | |
| }); | |
| }); | |
| } | |
| execute(fn); | |
| return deferred.promise; | |
| } |
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
| // ------- Test -------------------- | |
| function logAsync(message) { | |
| var deferred = $.Deferred(); | |
| console.log(message); | |
| return deferred.resolve().promise(); | |
| } | |
| function Foo(description) { | |
| this.description = description; | |
| this.succeedsAtThirdAttempt = succeedsAtThirdAttempt; | |
| var attempts = 0; | |
| function succeedsAtThirdAttempt() { | |
| attempts++; | |
| var deferred = $.Deferred(); | |
| if (attempts < 3) { | |
| console.log(attempts + " " + description + " rejecting.."); | |
| setTimeout(deferred.reject, 1000); | |
| } else { | |
| console.log(attempts + " " + description + " resolving.."); | |
| setTimeout(deferred.resolve, 1000); | |
| } | |
| return deferred.promise(); | |
| } | |
| } | |
| // should fail | |
| var foo = new Foo("should fail"); | |
| retry(2, 200, foo.succeedsAtThirdAttempt) | |
| .then(function (result) { | |
| return logAsync("Succeeded " + foo.description); | |
| }, function (result) { | |
| return logAsync("Failed " + foo.description); | |
| }). | |
| then(function () { | |
| // should succeed | |
| var foo = new Foo("should succeed"); | |
| retry(5, 200, foo.succeedsAtThirdAttempt) | |
| .then(function (result) { | |
| return logAsync("Succeeded " + foo.description); | |
| }, function (result) { | |
| return logAsync("Failed " + foo.description); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment