Last active
August 29, 2015 14:05
-
-
Save GavinJoyce/54c03bd4048a8f64742e to your computer and use it in GitHub Desktop.
Simple spike of an incremental backoff for Ember.js
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
// **EDIT**: I've created an ember-cli addon for this here: | |
// | |
// https://github.com/GavinJoyce/ember-backoff | |
var delay = function(milliseconds) { | |
milliseconds = milliseconds || 2000; | |
return new Em.RSVP.Promise(function(resolve) { | |
Em.run.later(this, function() { | |
resolve(); | |
}, milliseconds); | |
}); | |
}; | |
var retryWithBackoff = function(callback, retryCountBeforeFailure, waitInMilliseconds) { | |
if(Em.empty(retryCountBeforeFailure)) { | |
retryCountBeforeFailure = 5; | |
} | |
waitInMilliseconds = waitInMilliseconds || 250; | |
return callback().catch(function(reason) { | |
if (retryCountBeforeFailure-- > 1) { | |
waitInMilliseconds = waitInMilliseconds * 2; | |
return delay(waitInMilliseconds).then(function() { | |
return retryWithBackoff(callback, retryCountBeforeFailure, waitInMilliseconds); | |
}); | |
} | |
throw reason; | |
}); | |
} |
Author
GavinJoyce
commented
Aug 31, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment