diff options
author | Alexander Early <alexander.early@gmail.com> | 2018-09-02 20:22:58 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2018-09-02 20:22:58 -0700 |
commit | 0174f3f85b47a7a940865643608482d65fcd62c4 (patch) | |
tree | 448e085fa778d54e2e7e66641751d781b61ab257 | |
parent | 3a30a9f046859195205f05d36cce5011c14cba98 (diff) | |
download | async-0174f3f85b47a7a940865643608482d65fcd62c4.tar.gz |
awaitable parallel/race
-rw-r--r-- | lib/internal/parallel.js | 7 | ||||
-rw-r--r-- | lib/parallel.js | 7 | ||||
-rw-r--r-- | lib/parallelLimit.js | 3 | ||||
-rw-r--r-- | lib/race.js | 8 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 36 |
5 files changed, 49 insertions, 12 deletions
diff --git a/lib/internal/parallel.js b/lib/internal/parallel.js index 8abf0d1..790b6e8 100644 --- a/lib/internal/parallel.js +++ b/lib/internal/parallel.js @@ -1,9 +1,8 @@ import isArrayLike from './isArrayLike'; -import noop from './noop'; import wrapAsync from './wrapAsync'; +import awaitify from './awaitify' -export default function _parallel(eachfn, tasks, callback) { - callback = callback || noop; +export default awaitify((eachfn, tasks, callback) => { var results = isArrayLike(tasks) ? [] : {}; eachfn(tasks, (task, key, taskCb) => { @@ -15,4 +14,4 @@ export default function _parallel(eachfn, tasks, callback) { taskCb(err); }); }, err => callback(err, results)); -} +}, 3) diff --git a/lib/parallel.js b/lib/parallel.js index edf8be4..fafae9d 100644 --- a/lib/parallel.js +++ b/lib/parallel.js @@ -1,5 +1,5 @@ import eachOf from './eachOf'; -import parallel from './internal/parallel'; +import _parallel from './internal/parallel'; /** * Run the `tasks` collection of functions in parallel, without waiting until @@ -34,6 +34,7 @@ import parallel from './internal/parallel'; * functions have completed successfully. This function gets a results array * (or object) containing all the result arguments passed to the task callbacks. * Invoked with (err, results). + * @returns {Promise} a promise, if a callback is not passed * * @example * async.parallel([ @@ -70,6 +71,6 @@ import parallel from './internal/parallel'; * // results is now equals to: {one: 1, two: 2} * }); */ -export default function parallelLimit(tasks, callback) { - parallel(eachOf, tasks, callback); +export default function parallel(tasks, callback) { + return _parallel(eachOf, tasks, callback); } diff --git a/lib/parallelLimit.js b/lib/parallelLimit.js index 9a857b8..ee4335c 100644 --- a/lib/parallelLimit.js +++ b/lib/parallelLimit.js @@ -19,7 +19,8 @@ import parallel from './internal/parallel'; * functions have completed successfully. This function gets a results array * (or object) containing all the result arguments passed to the task callbacks. * Invoked with (err, results). + * @returns {Promise} a promise, if a callback is not passed */ export default function parallelLimit(tasks, limit, callback) { - parallel(eachOfLimit(limit), tasks, callback); + return parallel(eachOfLimit(limit), tasks, callback); } diff --git a/lib/race.js b/lib/race.js index 0593a72..05101a3 100644 --- a/lib/race.js +++ b/lib/race.js @@ -1,6 +1,6 @@ -import noop from './internal/noop'; import once from './internal/once'; import wrapAsync from './internal/wrapAsync'; +import awaitify from './internal/awaitify'; /** * Runs the `tasks` array of functions in parallel, without waiting until the @@ -38,11 +38,13 @@ import wrapAsync from './internal/wrapAsync'; * // the result will be equal to 'two' as it finishes earlier * }); */ -export default function race(tasks, callback) { - callback = once(callback || noop); +function race(tasks, callback) { + callback = once(callback); if (!Array.isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions')); if (!tasks.length) return callback(); for (var i = 0, l = tasks.length; i < l; i++) { wrapAsync(tasks[i])(callback); } } + +export default awaitify(race, 2) diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 45b7039..5a8df07 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -444,7 +444,6 @@ module.exports = function () { ]) }); - it('should return a Promise: forever', async () => { expect (async.forever.name).to.contain('forever') const calls = [] @@ -462,4 +461,39 @@ module.exports = function () { expect(calls).to.eql([0, 1, 2, 3, 4]) expect(err).to.be.an('error') }); + + it('should return a Promise: parallel', async () => { + expect (async.parallel.name).to.contain('parallel') + const calls = [] + await async.parallel([ + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + ]) + expect(calls).to.eql([1, 1, 1, 1]) + }); + it('should return a Promise: parallelLimit', async () => { + expect (async.parallelLimit.name).to.contain('parallelLimit') + const calls = [] + await async.parallelLimit([ + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + ], 2) + expect(calls).to.eql([1, 1, 1, 1]) + }); + + it('should return a Promise: race', async () => { + expect (async.race.name).to.contain('race') + const calls = [] + await async.race([ + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + async () => { await Promise.resolve(); calls.push(1) }, + ], 2) + expect(calls).to.eql([1, 1, 1, 1]) + }); }; |