diff options
author | Alex Early <alexander.early@gmail.com> | 2019-05-19 17:19:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 17:19:10 -0700 |
commit | 70624e42b59194b3e5d51d201ad5be344f57c3a6 (patch) | |
tree | 4244aa5a8e7ad91fc81a627a04ca73c929ba949a | |
parent | afe6be443b95bd55fdffba4eee210923f109c3d8 (diff) | |
parent | 667e71760e7f2dd558e868796bd6e6008a3bac16 (diff) | |
download | async-70624e42b59194b3e5d51d201ad5be344f57c3a6.tar.gz |
Merge branch 'master' into awaitable-queues
-rw-r--r-- | lib/internal/applyEach.js | 13 | ||||
-rw-r--r-- | test/applyEach.js | 39 | ||||
-rw-r--r-- | test/es2017/asyncFunctions.js | 4 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 17 |
4 files changed, 27 insertions, 46 deletions
diff --git a/lib/internal/applyEach.js b/lib/internal/applyEach.js index f95d8ac..c08a5eb 100644 --- a/lib/internal/applyEach.js +++ b/lib/internal/applyEach.js @@ -1,17 +1,14 @@ -import initialParams from './initialParams'; import wrapAsync from './wrapAsync'; +import awaitify from './awaitify' -export default function applyEach(eachfn) { - return function(fns, ...callArgs) { - var go = initialParams(function(args, callback) { +export default function (eachfn) { + return function applyEach(fns, ...callArgs) { + const go = awaitify(function (callback) { var that = this; return eachfn(fns, (fn, cb) => { - wrapAsync(fn).apply(that, args.concat(cb)); + wrapAsync(fn).apply(that, callArgs.concat(cb)); }, callback); }); - if (callArgs.length) { - return go.apply(this, callArgs); - } return go; }; } diff --git a/test/applyEach.js b/test/applyEach.js index 4a67ee4..a5fce95 100644 --- a/test/applyEach.js +++ b/test/applyEach.js @@ -27,7 +27,7 @@ describe('applyEach', () => { cb(null, 3); }, 18); }; - async.applyEach([one, two, three], 5, (err, results) => { + async.applyEach([one, two, three], 5)((err, results) => { assert(err === null, err + " passed instead of 'null'"); expect(call_order).to.eql(['two', 'one', 'three']); expect(results).to.eql([1, 2, 3]); @@ -51,7 +51,7 @@ describe('applyEach', () => { throw new Error('third task - should not get here'); } - async.applyEach({one, two, three}, 5, () => { + async.applyEach({one, two, three}, 5)(() => { throw new Error('final callback - should not get here'); }); @@ -84,7 +84,7 @@ describe('applyEach', () => { cb(null, 3); }, 15); } - async.applyEachSeries([one, two, three], 5, (err, results) => { + async.applyEachSeries([one, two, three], 5)((err, results) => { assert(err === null, err + " passed instead of 'null'"); expect(call_order).to.eql(['one', 'two', 'three']); expect(results).to.eql([1, 2, 3]); @@ -109,7 +109,7 @@ describe('applyEach', () => { function three(/*, cb */) { throw new Error('third task - should not get here'); } - async.applyEachSeries([one, two, three], 5, () => { + async.applyEachSeries([one, two, three], 5)(() => { throw new Error('final callback - should not get here'); }); @@ -118,35 +118,4 @@ describe('applyEach', () => { done(); }, 25); }); - - it('applyEach partial application', (done) => { - var call_order = []; - var one = function (val, cb) { - expect(val).to.equal(5); - setTimeout(() => { - call_order.push('one'); - cb(null, 1); - }, 10); - }; - var two = function (val, cb) { - expect(val).to.equal(5); - setTimeout(() => { - call_order.push('two'); - cb(null, 2); - }, 5); - }; - var three = function (val, cb) { - expect(val).to.equal(5); - setTimeout(() => { - call_order.push('three'); - cb(null, 3); - }, 15); - }; - async.applyEach([one, two, three])(5, (err, results) => { - if (err) throw err; - expect(call_order).to.eql(['two', 'one', 'three']); - expect(results).to.eql([1, 2, 3]); - done(); - }); - }); }); diff --git a/test/es2017/asyncFunctions.js b/test/es2017/asyncFunctions.js index 805c666..bf0d708 100644 --- a/test/es2017/asyncFunctions.js +++ b/test/es2017/asyncFunctions.js @@ -272,14 +272,14 @@ module.exports = function () { */ it('should handle async functions in applyEach', (done) => { - async.applyEach([asyncIdentity, asyncIdentity])(input, (err, result) => { + async.applyEach([asyncIdentity, asyncIdentity], input)((err, result) => { expect(result).to.eql([input, input]); done(err); }); }); it('should handle async functions in applyEachSeries', (done) => { - async.applyEachSeries([asyncIdentity, asyncIdentity])(input, (err, result) => { + async.applyEachSeries([asyncIdentity, asyncIdentity], input)((err, result) => { expect(result).to.eql([input, input]); done(err); }); diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 4517fe7..e8adf7b 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -316,7 +316,22 @@ module.exports = function () { * Control flow */ - // TODO: figure out to do with applyEach + it('should return a Promise: applyEach', async () => { + const calls = [] + await async.applyEach([ + async (v, x) => { calls.push(v, x) }, + async (v, x) => { calls.push(v, x) } + ], 5, 6)(); + expect(calls).to.eql([5, 6, 5, 6]) + }) + it('should return a Promise: applyEachSeries', async () => { + const calls = [] + await async.applyEachSeries([ + async (v, x) => { calls.push(v, x) }, + async (v, x) => { calls.push(v, x) } + ], 5, 6)(); + expect(calls).to.eql([5, 6, 5, 6]) + }) it('should return a Promise: auto', async () => { expect (async.auto.name).to.contain('auto') |