diff options
author | Alexander Early <alexander.early@gmail.com> | 2018-09-02 18:30:38 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2018-09-02 18:30:38 -0700 |
commit | 4c5358987647bc25792b6b89a0dfd03dcf6bed59 (patch) | |
tree | e81951a38a9bf8361e35b52e14b07f20c4929a95 | |
parent | 7df6325fe514df2d1559b624e20876bac48b39ab (diff) | |
download | async-4c5358987647bc25792b6b89a0dfd03dcf6bed59.tar.gz |
awaitable times
-rw-r--r-- | lib/times.js | 6 | ||||
-rw-r--r-- | lib/timesLimit.js | 5 | ||||
-rw-r--r-- | lib/timesSeries.js | 6 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 20 |
4 files changed, 30 insertions, 7 deletions
diff --git a/lib/times.js b/lib/times.js index ebd336c..15e798f 100644 --- a/lib/times.js +++ b/lib/times.js @@ -1,5 +1,4 @@ import timesLimit from './timesLimit'; -import doLimit from './internal/doLimit'; /** * Calls the `iteratee` function `n` times, and accumulates results in the same @@ -15,6 +14,7 @@ import doLimit from './internal/doLimit'; * @param {AsyncFunction} iteratee - The async function to call `n` times. * Invoked with the iteration index and a callback: (n, next). * @param {Function} callback - see {@link module:Collections.map}. + * @returns {Promise} a promise, if no callback is provided * @example * * // Pretend this is some complicated async factory @@ -33,4 +33,6 @@ import doLimit from './internal/doLimit'; * // we should now have 5 users * }); */ -export default doLimit(timesLimit, Infinity); +export default function times (n, iteratee, callback) { + return timesLimit(n, Infinity, iteratee, callback) +} diff --git a/lib/timesLimit.js b/lib/timesLimit.js index bb4a1fe..8880ccf 100644 --- a/lib/timesLimit.js +++ b/lib/timesLimit.js @@ -17,8 +17,9 @@ import wrapAsync from './internal/wrapAsync'; * @param {AsyncFunction} iteratee - The async function to call `n` times. * Invoked with the iteration index and a callback: (n, next). * @param {Function} callback - see [async.map]{@link module:Collections.map}. + * @returns {Promise} a promise, if no callback is provided */ -export default function timeLimit(count, limit, iteratee, callback) { +export default function timesLimit(count, limit, iteratee, callback) { var _iteratee = wrapAsync(iteratee); - mapLimit(range(count), limit, _iteratee, callback); + return mapLimit(range(count), limit, _iteratee, callback); } diff --git a/lib/timesSeries.js b/lib/timesSeries.js index 17ec0ca..b56f6ec 100644 --- a/lib/timesSeries.js +++ b/lib/timesSeries.js @@ -1,5 +1,4 @@ import timesLimit from './timesLimit'; -import doLimit from './internal/doLimit'; /** * The same as [times]{@link module:ControlFlow.times} but runs only a single async operation at a time. @@ -14,5 +13,8 @@ import doLimit from './internal/doLimit'; * @param {AsyncFunction} iteratee - The async function to call `n` times. * Invoked with the iteration index and a callback: (n, next). * @param {Function} callback - see {@link module:Collections.map}. + * @returns {Promise} a promise, if no callback is provided */ -export default doLimit(timesLimit, 1); +export default function timesSeries (n, iteratee, callback) { + return timesLimit(n, 1, iteratee, callback) +} diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 5f7626f..7967d90 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -280,13 +280,31 @@ module.exports = function () { expect(calls).to.eql([[1], [2], [3]]) }); + it('should return a Promise: times', async () => { + expect (async.times.name).to.contain('times') + const calls = [] + await async.times(3, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[0], [1], [2]]) + }); + it('should return a Promise: timesSeries', async () => { + expect (async.timesSeries.name).to.contain('timesSeries') + const calls = [] + await async.timesSeries(3, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[0], [1], [2]]) + }); + it('should return a Promise: timesLimit', async () => { + expect (async.timesLimit.name).to.contain('timesLimit') + const calls = [] + await async.timesLimit(3, 1, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[0], [1], [2]]) + }); + it('should return a Promise: transform', async () => { expect (async.transform.name).to.contain('transform') const calls = [] await async.transform(inputObj, 1, async (...args) => calls.push(args)); expect(calls).to.eql([[1, 1, 'a'], [1, 2, 'b'], [1, 3, 'c']]) }); - it('should return a Promise: transform (2 args)', async () => { expect (async.transform.name).to.contain('transform') const calls = [] |