diff --git a/src/doUntil.js b/src/doUntil.js index 0a6e8c4..7f57db2 100644 --- a/src/doUntil.js +++ b/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) ); -}; +}); diff --git a/src/until.js b/src/until.js index 52e0a94..53a3acf 100644 --- a/src/until.js +++ b/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 @@ -11,4 +12,4 @@ export default function until(condition, task, ...args) { : until(condition, task, result) ); }); -}; +}); diff --git a/test/doUntil.js b/test/doUntil.js index 0da6ee6..d8ddf85 100644 --- a/test/doUntil.js +++ b/test/doUntil.js @@ -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( diff --git a/test/until.js b/test/until.js index 47634eb..b9829c0 100644 --- a/test/until.js +++ b/test/until.js @@ -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(