Skip to content

Instantly share code, notes, and snippets.

@advait
Created August 21, 2014 19:44
Show Gist options
  • Select an option

  • Save advait/7665d91c5d4b0e3cf632 to your computer and use it in GitHub Desktop.

Select an option

Save advait/7665d91c5d4b0e3cf632 to your computer and use it in GitHub Desktop.

Revisions

  1. advait renamed this gist Aug 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. advait created this gist Aug 21, 2014.
    27 changes: 27 additions & 0 deletions backoff
    Original 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();
    });
    };