Skip to content

Commit

Permalink
Update retry and retryable to support promised arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jgornick committed Oct 13, 2016
1 parent 947f7d3 commit 8b0a403
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/retry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import isPlainObject from './isPlainObject';
import tryFn from './tryFn';
import promised from './promised';
import PromiseBreak from './promiseBreak';

export default function retry(opts, ...args) {
export default promised(function retry(opts, ...args) {
let task;

if (typeof opts == 'function') {
Expand Down Expand Up @@ -71,4 +72,4 @@ export default function retry(opts, ...args) {
}
throw error;
});
};
});
33 changes: 25 additions & 8 deletions test/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,32 @@ describe('retry', function() {
]);
});

it('throws with invalid times value', function(done) {
expect(async.retry.bind(null, 'foo', delayedTask(1, failTask)))
.to.throw(Error, `Invalid times option value of "foo"`)
done();
it('supports promised arguments', function() {
let order = [];
let counter = { count: 0 };
const p = async.retry(
Promise.resolve(delayedTask(1, successTask)),
new Promise(resolve => setTimeout(resolve.bind(null, order), 25)),
new Promise(resolve => setTimeout(resolve.bind(null, counter), 25))
);

return Promise.all([
p.should.eventually.deep.equal(3),
p.then(() => order.should.deep.equal([1, 2, 3]))
]);
});

it('throws with invalid times option', function(done) {
expect(async.retry.bind(null, { times: 'foo' }, delayedTask(1, failTask)))
.to.throw(Error, `Invalid times option value of "foo"`)
done();
it('rejects with invalid times value', function() {
const p = async.retry('foo', delayedTask(1, failTask));
return Promise.all([
p.should.eventually.be.rejectedWith(Error, `Invalid times option value of "foo"`)
]);
});

it('rejects with invalid times option', function() {
const p = async.retry({ times: 'foo' }, delayedTask(1, failTask));
return Promise.all([
p.should.eventually.be.rejectedWith(Error, `Invalid times option value of "foo"`)
]);
});
});
20 changes: 12 additions & 8 deletions test/retryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,20 @@ describe('retryable', function() {
]);
});

it('throws with invalid times value', function(done) {
expect(async.retryable('foo', delayedTask(1, failTask)))
.to.throw(Error, `Invalid times option value of "foo"`)
done();
it('rejects with invalid times value', function() {
const f = async.retryable('foo', delayedTask(1, failTask));
const p = f();
return Promise.all([
p.should.eventually.be.rejectedWith(Error, `Invalid times option value of "foo"`)
]);
});

it('throws with invalid times option', function(done) {
expect(async.retryable({ times: 'foo' }, delayedTask(1, failTask)))
.to.throw(Error, `Invalid times option value of "foo"`)
done();
it('throws with invalid times option', function() {
const f = async.retryable({ times: 'foo' }, delayedTask(1, failTask));
const p = f();
return Promise.all([
p.should.eventually.be.rejectedWith(Error, `Invalid times option value of "foo"`)
]);
});

});

0 comments on commit 8b0a403

Please sign in to comment.