Created
August 21, 2014 19:44
-
-
Save advait/7665d91c5d4b0e3cf632 to your computer and use it in GitHub Desktop.
Revisions
-
advait renamed this gist
Aug 21, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
advait created this gist
Aug 21, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ /** * Given a function promiseFunctionToRetry that returns a Promise, repeatedly * call the function with a fibonacci backoff until it results in a resolved * Promise. * @param {function} promiseFunctionToRetry a function that returns a Promise * that can potentially reject. This may be called multiple times. * @param {number?} backoff An optional starting backoff in ms (default 1000) * @return {Promise} guaranteed to never reject */ var backoff = function(promiseFunctionToRetry, backoff) { return new Promise(function(resolve) { var currentBackoff = backoff || 1000; var backoffStep = currentBackoff; var retry = function() { promiseFunctionToRetry() .then(resolve, function() { setTimeout(retry, currentBackoff); var temp = currentBackoff; currentBackoff += backoffStep; backoffStep = temp; }); }; retry(); }); };