npm

Bring the best of OSS JavaScript development to your projects with npm Orgs - private packages & team management tools.Learn more »

backoff-promise

1.0.0 • Public • Published

backoff-promise

Greenkeeper badge Circle CI

An exponential backoff for promises

Example 1

var i = 0,
  Backoff = require('backoff-promise'),
  backoff = new Backoff();
 
var myPromise = function () {
  return new Promise(function (resolve, reject) {
    if (i++ < 5) {
      reject(new Error('transient-error'));
    } else {
      resolve('foo');
    }
  });
};
 
return backoff.run(function () {
  return myPromise();
});

Example 2: Different types of errors

var i = 0,
  Backoff = require('backoff-promise'),
  backoff = new Backoff();
 
var myPromise = function () {
  return new Promise(function (resolve, reject) {
    if (i++ < 3) {
      reject(new Error('transient-error'));
    } else {
      reject(new Error('permanent-error'));
    }
  });
};
 
var shouldRetry = function (err) {
  // Permanent error? If so, then stop retrying
  if (err.message === 'permanent-error') {
    throw err;
  }
};
 
return backoff.run(function () {
  return myPromise();
}, shouldRetry).catch(function (err) {
  // err.message = 'transient-error'
});

API

var backoff = Backoff(startingRetryAfterMSecs, maxRetryAfterMSecs, backoffFactor, maxRetries)

install

npm i backoff-promise

Downloadsweekly downloads

2,283

version

1.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability