lib/promise.js
/*
 * Retry the function fn up to opts.max times until it successfully completes
 * without an error. Pause opts.backoff * retry miliseconds between tries.
 */
Retry the function fn up to opts.max times until it successfully completes
without an error. Pause opts.backoff * retry miliseconds between tries.
Retry the function fn up to opts . max times until it successfully completes without an error . Pause opts . backoff * retry miliseconds between tries .
function retry(promise, fn, opts) {
  return promise.then(function() {
    opts = opts || {};
    opts.max = opts.max || 5;
    opts.backoff = opts.backoff || 500;
    var rec = function(counter) {
      return Promise.try(function() {
        return fn(counter);
      })
      .catch(function(err) {
        if (counter < opts.max) {
          return Promise.delay(opts.backoff * counter)
          .then(function() {
            return rec(counter + 1);
          });
        } else {
          throw new VError(
            err,
            'Failed after %s retries. %s',
            opts.max,
            JSON.stringify(opts)
          );
        }
      });
    };
    return rec(1);
  });
}
function retry ( promise , fn , opts ) { return promise . then ( function ( ) { opts = opts || { } ; opts . max = opts . max || 5 ; opts . backoff = opts . backoff || 500 ; var rec = function ( counter ) { return Promise . try ( function ( ) { return fn ( counter ) ; } ) . catch ( function ( err ) { if ( counter < opts . max ) { return Promise . delay ( opts . backoff * counter ) . then ( function ( ) { return rec ( counter + 1 ) ; } ) ; } else { throw new VError ( err , 'Failed after %s retries. %s' , opts . max , JSON . stringify ( opts ) ) ; } } ) ; } ; return rec ( 1 ) ; } ) ; }
Retry the function fn up to opts.max times until it successfully completes
 without an error.
Retry the function fn up to opts . max times until it successfully completes without an error .
// Piggy back off of previous promise. // Setup default options. // Recursive function. // Call function fn within the context of a Promise. // Handle errors. // If we haven't reached max retries, delay for a short while and // then retry. // No retries left so wrap and throw the error. // Init recursive function.