Skip to content

Commit

Permalink
feat: options.Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed May 19, 2018
1 parent 6b0a315 commit 1e0e7f9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -25,7 +25,8 @@ return retry(function (options) {
backoffBase: 1000 // Initial backoff duration in ms. Default: 100,
backoffExponent: 1.5 // Exponent to increase backoff each try. Default: 1.1
report: warningFn, // the function used for reporting; must have a (string, object) argument signature, where string is the message that will passed in by retry-as-promised, and the object will be this configuration object + the $current property
name: 'SourceX' // if user supplies string, it will be used when composing error/reporting messages; else if retry gets a callback, uses callback name in erroring/reporting; else (default) uses litteral string 'unknown'
name: 'SourceX', // if user supplies string, it will be used when composing error/reporting messages; else if retry gets a callback, uses callback name in erroring/reporting; else (default) uses litteral string 'unknown'
Promise: Promise // Promise instance to create promises with
});
```

Expand Down
11 changes: 7 additions & 4 deletions index.js
@@ -1,6 +1,6 @@
var debug = require('debug')('retry-as-promised')
, error = require('debug')('retry-as-promised:error')
, Promise = require('bluebird');
, Bluebird = require('bluebird');

module.exports = function retryAsPromised(callback, options) {
if (!callback || !options) throw new Error('retry-as-promised must be passed a callback and a options set or a number');
Expand All @@ -18,12 +18,15 @@ module.exports = function retryAsPromised(callback, options) {
backoffBase: options.backoffBase === undefined ? 100 : options.backoffBase,
backoffExponent: options.backoffExponent || 1.1,
report: options.report || null,
name: options.name || callback.name || 'unknown'
name: options.name || callback.name || 'unknown',
Promise: options.Promise || Bluebird
};

var Promise = options.Promise || Bluebird;

// Massage match option into array so we can blindly treat it as such later
if (!Array.isArray(options.match)) options.match = [options.match];

if(options.report) options.report('Trying ' + options.name + ' #' + options.$current + ' at ' + new Date().toLocaleTimeString(), options);

return new Promise(function (resolve, reject) {
Expand All @@ -32,7 +35,7 @@ module.exports = function retryAsPromised(callback, options) {
if (options.timeout) {
timeout = setTimeout(function () {
if (backoffTimeout) clearTimeout(backoffTimeout);
reject(Promise.TimeoutError( options.name + ' timed out'));
reject(Bluebird.TimeoutError( options.name + ' timed out'));
}, options.timeout);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/mickhansen/retry-as-promised",
"dependencies": {
"bluebird": "^3.4.6",
"bluebird": "^3.5.1",
"debug": "^2.6.9"
},
"devDependencies": {
Expand Down
57 changes: 54 additions & 3 deletions test/bluebird.test.js
Expand Up @@ -70,7 +70,7 @@ describe('bluebird', function () {
});
});

describe('timeout', function () {
describe('options.timeout', function () {
it('should throw if reject on first attempt', function () {
return expect(retry(function () {
return Promise.delay(2000);
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('bluebird', function () {
});
});

describe('match', function () {
describe('options.match', function () {
it('should continue retry while error is equal to match string', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('bluebird', function () {
});
});

describe('backoff', function () {
describe('options.backoff', function () {
it('should resolve after 5 retries and an eventual delay over 1800ms using default backoff', function () {
var startTime = moment();
var callback = sinon.stub();
Expand Down Expand Up @@ -204,4 +204,55 @@ describe('bluebird', function () {
})).to.eventually.be.rejectedWith(Promise.TimeoutError);
});
});

describe('options.Promise', function () {
it('should use Promise instance for creating promises', function () {
var bluebird = Promise.getNewLibraryCopy();
var reply = retry(function () {
return bluebird.delay(2000);
}, {
Promise: bluebird
});

expect(reply).to.be.an.instanceOf(bluebird);
expect(reply).not.to.be.an.instanceOf(Promise);

return reply;
});

describe('timeout', function () {
it('should be able to handle timeout error', function () {
var bluebird = Promise.getNewLibraryCopy();
var reply = retry(function () {
return bluebird.delay(2000);
}, {
Promise: bluebird,
timeout: 1000
});

return Promise.all([
expect(reply).to.eventually.be.rejectedWith(bluebird.TimeoutError),
expect(reply).to.eventually.be.rejectedWith(Promise.TimeoutError)
]);
});
});

describe('retry', function () {
it('should return correct promise instance after retries', function () {
var bluebird = Promise.getNewLibraryCopy();
var reply = retry(function () {
return bluebird.delay(2000).then(() => {
throw new Error('errr');
})
}, {
Promise: bluebird
});

expect(reply).to.be.an.instanceOf(bluebird);
expect(reply).not.to.be.an.instanceOf(Promise);

return expect(reply).to.be.rejectedWith('errr');
});
});
});
});

0 comments on commit 1e0e7f9

Please sign in to comment.