Skip to content

Commit

Permalink
Update until and doUntil 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 e61bf26 commit e809fe4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/doUntil.js
@@ -1,11 +1,12 @@
import tryFn from './tryFn';
import promised from './promised';
import until from './until';

export default function doUntil(task, condition, ...args) {
export default promised(function doUntil(task, condition, ...args) {
return tryFn(task, ...args)
.then((result) =>
Array.isArray(result)
? until(condition, task, ...result)
: until(condition, task, result)
);
};
});
5 changes: 3 additions & 2 deletions src/until.js
@@ -1,6 +1,7 @@
import tryFn from './tryFn';
import promised from './promised';

export default function until(condition, task, ...args) {
export default promised(function until(condition, task, ...args) {
return tryFn(condition, ...args)
.then((conditionResult) => {
return conditionResult
Expand All @@ -11,4 +12,4 @@ export default function until(condition, task, ...args) {
: until(condition, task, result)
);
});
};
});
44 changes: 44 additions & 0 deletions test/doUntil.js
Expand Up @@ -103,6 +103,50 @@ describe('doUntil', function() {
]);
});

it('supports promised arguments', function() {
let order = [];
const p = async.doUntil(
new Promise(resolve => setTimeout(
resolve.bind(
null,
count => new Promise(resolve => setTimeout(_ => {
order.push(`task${count}`);
count++;
resolve(count);
}, 25))
),
25
)),
new Promise(resolve => setTimeout(
resolve.bind(
null,
count => new Promise(resolve => setTimeout(_ => {
order.push(`condition${count}`);
resolve(count == 5);
}, 25))
),
25
)),
Promise.resolve(0)
);

return Promise.all([
p.should.eventually.equal(undefined),
p.then(() => order.should.deep.equal([
'task0',
'condition1',
'task1',
'condition2',
'task2',
'condition3',
'task3',
'condition4',
'task4',
'condition5'
]))
]);
});

it('rejects in delayed task', function() {
let order = [];
const p = async.doUntil(
Expand Down
46 changes: 46 additions & 0 deletions test/until.js
Expand Up @@ -142,6 +142,52 @@ describe('until', function() {
]);
});

it('supports promised arguments', function() {
let order = [];
const p = async.until(
new Promise(resolve => setTimeout(
resolve.bind(
null,
count => new Promise(resolve => setTimeout(_ => {
order.push(`condition${count}`);
resolve(count == 5);
}, 25))
),
25
)),
new Promise(resolve => setTimeout(
resolve.bind(
null,
count => new Promise(resolve => setTimeout(_ => {
order.push(`task${count}`);
count++;
resolve(count);
}, 25))
),
25
)),
Promise.resolve(0)
);

return Promise.all([
p.should.eventually.equal(undefined),
p.then(() => order.should.deep.equal([
'condition0',
'task0',
'condition1',
'task1',
'condition2',
'task2',
'condition3',
'task3',
'condition4',
'task4',
'condition5'
]))
]);
});


it('rejects in delayed task', function() {
let order = [];
const p = async.until(
Expand Down

0 comments on commit e809fe4

Please sign in to comment.