diff options
author | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:09:06 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:09:06 -0700 |
commit | 68363dc92e4601418873d974d08c763c9f969432 (patch) | |
tree | bfe4bfe79a52ff4744a06a84cb78e2e75280f03a | |
parent | 6e07f6c5f4f2abe844d47651a03a449f864a5190 (diff) | |
download | async-68363dc92e4601418873d974d08c763c9f969432.tar.gz |
awaitable every/filter
-rw-r--r-- | lib/every.js | 9 | ||||
-rw-r--r-- | lib/everyLimit.js | 9 | ||||
-rw-r--r-- | lib/everySeries.js | 11 | ||||
-rw-r--r-- | lib/filter.js | 11 | ||||
-rw-r--r-- | lib/filterLimit.js | 11 | ||||
-rw-r--r-- | lib/filterSeries.js | 11 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 38 |
7 files changed, 84 insertions, 16 deletions
diff --git a/lib/every.js b/lib/every.js index be2bc4a..a5ce971 100644 --- a/lib/every.js +++ b/lib/every.js @@ -1,5 +1,6 @@ import createTester from './internal/createTester'; -import doParallel from './internal/doParallel'; +import eachOf from './eachOf' +import awaitify from './internal/awaitify' /** * Returns `true` if every element in `coll` satisfies an async test. If any @@ -19,6 +20,7 @@ import doParallel from './internal/doParallel'; * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Result will be either `true` or `false` * depending on the values of the async tests. Invoked with (err, result). + * @returns {Promise} a promise, if no callback provided * @example * * async.every(['file1','file2','file3'], function(filePath, callback) { @@ -29,4 +31,7 @@ import doParallel from './internal/doParallel'; * // if result is true then every file exists * }); */ -export default doParallel(createTester(bool => !bool, res => !res)); +function every(coll, iteratee, callback) { + return createTester(bool => !bool, res => !res)(eachOf, coll, iteratee, callback) +} +export default awaitify(every, 3); diff --git a/lib/everyLimit.js b/lib/everyLimit.js index 78ffa36..787c704 100644 --- a/lib/everyLimit.js +++ b/lib/everyLimit.js @@ -1,5 +1,6 @@ import createTester from './internal/createTester'; -import doParallelLimit from './internal/doParallelLimit'; +import eachOfLimit from './internal/eachOfLimit' +import awaitify from './internal/awaitify' /** * The same as [`every`]{@link module:Collections.every} but runs a maximum of `limit` async operations at a time. @@ -20,5 +21,9 @@ import doParallelLimit from './internal/doParallelLimit'; * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Result will be either `true` or `false` * depending on the values of the async tests. Invoked with (err, result). + * @returns {Promise} a promise, if no callback provided */ -export default doParallelLimit(createTester(bool => !bool, res => !res)); +function everyLimit(coll, limit, iteratee, callback) { + return createTester(bool => !bool, res => !res)(eachOfLimit(limit), coll, iteratee, callback) +} +export default awaitify(everyLimit, 4); diff --git a/lib/everySeries.js b/lib/everySeries.js index c450bff..07d2612 100644 --- a/lib/everySeries.js +++ b/lib/everySeries.js @@ -1,5 +1,6 @@ -import everyLimit from './everyLimit'; -import doLimit from './internal/doLimit'; +import createTester from './internal/createTester'; +import eachOfSeries from './eachOfSeries'; +import awaitify from './internal/awaitify'; /** * The same as [`every`]{@link module:Collections.every} but runs only a single async operation at a time. @@ -19,5 +20,9 @@ import doLimit from './internal/doLimit'; * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Result will be either `true` or `false` * depending on the values of the async tests. Invoked with (err, result). + * @returns {Promise} a promise, if no callback provided */ -export default doLimit(everyLimit, 1); +function everySeries(coll, iteratee, callback) { + return createTester(bool => !bool, res => !res)(eachOfSeries, coll, iteratee, callback) +} +export default awaitify(everySeries, 3); diff --git a/lib/filter.js b/lib/filter.js index 692802b..b0f3029 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,5 +1,6 @@ -import filter from './internal/filter'; -import doParallel from './internal/doParallel'; +import _filter from './internal/filter'; +import eachOf from './eachOf' +import awaitify from './internal/awaitify' /** * Returns a new array of all the values in `coll` which pass an async truth @@ -18,6 +19,7 @@ import doParallel from './internal/doParallel'; * with a boolean argument once it has completed. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Invoked with (err, results). + * @returns {Promise} a promise, if no callback provided * @example * * async.filter(['file1','file2','file3'], function(filePath, callback) { @@ -28,4 +30,7 @@ import doParallel from './internal/doParallel'; * // results now equals an array of the existing files * }); */ -export default doParallel(filter); +function filter (coll, iteratee, callback) { + return _filter(eachOf, coll, iteratee, callback) +} +export default awaitify(filter, 3); diff --git a/lib/filterLimit.js b/lib/filterLimit.js index 946002d..b421caa 100644 --- a/lib/filterLimit.js +++ b/lib/filterLimit.js @@ -1,5 +1,6 @@ -import filter from './internal/filter'; -import doParallelLimit from './internal/doParallelLimit'; +import _filter from './internal/filter'; +import eachOfLimit from './internal/eachOfLimit' +import awaitify from './internal/awaitify' /** * The same as [`filter`]{@link module:Collections.filter} but runs a maximum of `limit` async operations at a @@ -19,5 +20,9 @@ import doParallelLimit from './internal/doParallelLimit'; * with a boolean argument once it has completed. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Invoked with (err, results). + * @returns {Promise} a promise, if no callback provided */ -export default doParallelLimit(filter); +function filterLimit (coll, limit, iteratee, callback) { + return _filter(eachOfLimit(limit), coll, iteratee, callback) +} +export default awaitify(filterLimit, 4); diff --git a/lib/filterSeries.js b/lib/filterSeries.js index 908afbc..f53c3d8 100644 --- a/lib/filterSeries.js +++ b/lib/filterSeries.js @@ -1,5 +1,6 @@ -import filterLimit from './filterLimit'; -import doLimit from './internal/doLimit'; +import _filter from './internal/filter'; +import eachOfSeries from './eachOfSeries' +import awaitify from './internal/awaitify' /** * The same as [`filter`]{@link module:Collections.filter} but runs only a single async operation at a time. @@ -17,5 +18,9 @@ import doLimit from './internal/doLimit'; * with a boolean argument once it has completed. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished. Invoked with (err, results) + * @returns {Promise} a promise, if no callback provided */ -export default doLimit(filterLimit, 1); +function filterSeries (coll, iteratee, callback) { + return _filter(eachOfSeries, coll, iteratee, callback) +} +export default awaitify(filterSeries, 3); diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 00748ce..56cf07a 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -125,4 +125,42 @@ module.exports = function () { await async.detectLimit(input, 1, async (...args) => { calls.push(args); return args[0] === 3 }); expect(calls).to.eql([[1], [2], [3]]) }); + + it('should return a Promise: every', async () => { + expect (async.every.name).to.contain('every') + const calls = [] + await async.every(input, async (...args) => { calls.push(args); return args[0] !== 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: everySeries', async () => { + expect (async.everySeries.name).to.contain('everySeries') + const calls = [] + await async.everySeries(input, async (...args) => { calls.push(args); return args[0] !== 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: everyLimit', async () => { + expect (async.everyLimit.name).to.contain('everyLimit') + const calls = [] + await async.everyLimit(input, 1, async (...args) => { calls.push(args); return args[0] !== 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + + it('should return a Promise: filter', async () => { + expect (async.filter.name).to.contain('filter') + const calls = [] + await async.filter(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: filterSeries', async () => { + expect (async.filterSeries.name).to.contain('filterSeries') + const calls = [] + await async.filterSeries(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: filterLimit', async () => { + expect (async.filterLimit.name).to.contain('filterLimit') + const calls = [] + await async.filterLimit(inputObj, 1, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); }; |