Skip to content

Commit a2b4405

Browse files
committed
Merge pull request benbria#2 from benbria/fix-retry
Fix retry
2 parents f2627cc + 13d4cd9 commit a2b4405

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
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 = +options.interval;
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: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,32 @@ describe('retry', () => {
3939
[
4040
{options: {times: 5, interval: 10}, msg: 'times and interval'},
4141
{options: {times: 5}, msg: 'just times'},
42-
{options: {}, msg: 'neither times nor interval'}
42+
{options: {times: 5.4}, msg: 'just times'},
43+
{options: {times: Infinity}, msg: 'times = Infinity'},
44+
{options: {}, msg: 'neither times nor interval'},
45+
{options: {interval: Infinity}, msg: 'Should accept Infinity interval'}
4346
].forEach((args) => {
4447
it(`should accept first argument as an options hash with ${args.msg}`, () => {
4548
let retry = promiseTools.retry(args.options, getTest(5));
4649
return expect(retry).to.eventually.equal(5);
4750
});
4851
});
4952

53+
it('should call the default 5 times with no options provided', () => {
54+
let retry = promiseTools.retry(getTest(5));
55+
return expect(retry).to.eventually.equal(5);
56+
})
57+
58+
it('should throw when given a bogus `times` option', () => {
59+
let retry = promiseTools.retry('5', getTest(5));
60+
return expect(retry).to.be.eventually.rejected;
61+
})
62+
5063
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');
64+
expect(promiseTools.retry(undefined, getTest(1))).to.eventually.be.rejectedWith('Unsupported argument type for \'times\': undefined');
6065
});
66+
67+
it('should reject when called with nothing', () => {
68+
expect(promiseTools.retry()).to.eventually.be.rejectedWith('No parameters given');
69+
})
6170
});

0 commit comments

Comments
 (0)