Skip to content

Commit 591e6ab

Browse files
committed
Some fixes for retry()
1 parent f2627cc commit 591e6ab

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ exports.doWhilst = (fn, test) => {
208208

209209
/*
210210
* keep calling `fn` until it returns a non-error value, doesn't throw, or returns a Promise that resolves. `fn` will be
211-
* attempted `times` many times before rejecting. If `times` is given as `exports.FOREVER`, then `retry` will attempt to
211+
* attempted `times` many times before rejecting. If `times` is given as `Infinity`, then `retry` will attempt to
212212
* resolve forever (useful if you are just waiting for something to finish).
213213
* @param {Object|Number} options hash to provide `times` and `interval`. Defaults (times=5, interval=0). If this value
214214
* is a number, only `times` will be set.
@@ -222,16 +222,20 @@ exports.retry = (options, fn) => {
222222
let attempts = 0;
223223
let lastAttempt = null;
224224

225-
if ('number' === typeof(options)) {
226-
times = options;
225+
function makeTimeOptionError(value) {
226+
return new Error(`Unsupported argument type for \'times\': ${typeof(value)}`);
227227
}
228+
229+
if ('function' === typeof(options)) fn = options;
230+
else if ('number' === typeof(options)) times = +options;
228231
else if ('object' === typeof(options)) {
229-
if (options.times) {times = parseInt(options.times, 10);}
230-
if (options.interval) {interval = parseInt(options.interval, 10);}
231-
}
232-
else {
233-
throw new Error('Unsupported argument type for \'times\': ' + typeof(options));
232+
if ('number' === typeof(options.times)) times = +options.times;
233+
else if (options.times) return Promise.reject(makeTimeOptionError(options.times));
234+
235+
if (options.interval) interval = parseInt(options.interval, 10);
234236
}
237+
else if (options) return Promise.reject(makeTimeOptionError(options));
238+
else return Promise.reject(new Error('No parameters given'));
235239

236240
return new Promise((resolve, reject) => {
237241
let doIt = () => {

test/retry.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ describe('retry', () => {
3939
[
4040
{options: {times: 5, interval: 10}, msg: 'times and interval'},
4141
{options: {times: 5}, msg: 'just times'},
42+
{options: {times: 5.4}, msg: 'just times'},
43+
{options: {times: Infinity}, msg: 'times = Infinity'},
4244
{options: {}, msg: 'neither times nor interval'}
4345
].forEach((args) => {
4446
it(`should accept first argument as an options hash with ${args.msg}`, () => {
@@ -47,15 +49,21 @@ describe('retry', () => {
4749
});
4850
});
4951

52+
it('should call the default 5 times with no options provided', () => {
53+
let retry = promiseTools.retry(getTest(5));
54+
return expect(retry).to.eventually.equal(5);
55+
})
56+
57+
it('should throw when given a bogus `times` option', () => {
58+
let retry = promiseTools.retry('5', getTest(5));
59+
return expect(retry).to.be.eventually.rejected;
60+
})
61+
5062
it('should return an error with invalid options argument', () => {
51-
let p = Promise.resolve().then(() => {
52-
// had to do this for some reason. Otherwise `chai-as-promised` always passed erroneously.
53-
try {
54-
return promiseTools.retry(undefined, getTest(1));
55-
} catch (err) {
56-
throw err;
57-
}
58-
});
59-
expect(p).to.eventually.be.rejectedWith('Unsupported argument type for \'times\': undefined');
63+
expect(promiseTools.retry(undefined, getTest(1))).to.eventually.be.rejectedWith('Unsupported argument type for \'times\': undefined');
6064
});
65+
66+
it('should reject when called with nothing', () => {
67+
expect(promiseTools.retry()).to.eventually.be.rejectedWith('No parameters given');
68+
})
6169
});

0 commit comments

Comments
 (0)