diff options
| author | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:58:08 -0700 |
|---|---|---|
| committer | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:58:08 -0700 |
| commit | f12ce3988357b0a9d4b255acd51173d7c78f0e13 (patch) | |
| tree | 11f57d3d11ee76b11ad1072e72df42fdc5111be5 | |
| parent | ed2eb6c961177b853fc039be592930617a0b9190 (diff) | |
| download | async-f12ce3988357b0a9d4b255acd51173d7c78f0e13.tar.gz | |
awaitable some
| -rw-r--r-- | lib/internal/wrapAsync.js | 1 | ||||
| -rw-r--r-- | lib/some.js | 9 | ||||
| -rw-r--r-- | lib/someLimit.js | 9 | ||||
| -rw-r--r-- | lib/someSeries.js | 11 | ||||
| -rw-r--r-- | lib/sortBy.js | 7 | ||||
| -rw-r--r-- | test/es2017/awaitableFunctions.js | 27 |
6 files changed, 55 insertions, 9 deletions
diff --git a/lib/internal/wrapAsync.js b/lib/internal/wrapAsync.js index 5c3edd9..270cf8f 100644 --- a/lib/internal/wrapAsync.js +++ b/lib/internal/wrapAsync.js @@ -13,6 +13,7 @@ function isAsyncIterable(obj) { } function wrapAsync(asyncFn) { + if (typeof asyncFn !== 'function') throw new Error('expected a function') return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn; } diff --git a/lib/some.js b/lib/some.js index 65543bb..378ccdf 100644 --- a/lib/some.js +++ b/lib/some.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 at least one element in the `coll` satisfies an async test. @@ -21,6 +22,7 @@ import doParallel from './internal/doParallel'; * iteratee returns `true`, or 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.some(['file1','file2','file3'], function(filePath, callback) { @@ -31,4 +33,7 @@ import doParallel from './internal/doParallel'; * // if result is true then at least one of the files exists * }); */ -export default doParallel(createTester(Boolean, res => res)); +function some(coll, iteratee, callback) { + return createTester(Boolean, res => res)(eachOf, coll, iteratee, callback) +} +export default awaitify(some, 3); diff --git a/lib/someLimit.js b/lib/someLimit.js index fb6841c..2defb28 100644 --- a/lib/someLimit.js +++ b/lib/someLimit.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 [`some`]{@link module:Collections.some} but runs a maximum of `limit` async operations at a time. @@ -21,5 +22,9 @@ import doParallelLimit from './internal/doParallelLimit'; * iteratee returns `true`, or 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(Boolean, res => res)); +function someLimit(coll, limit, iteratee, callback) { + return createTester(Boolean, res => res)(eachOfLimit(limit), coll, iteratee, callback) +} +export default awaitify(someLimit, 4); diff --git a/lib/someSeries.js b/lib/someSeries.js index c05ca1f..7842124 100644 --- a/lib/someSeries.js +++ b/lib/someSeries.js @@ -1,5 +1,6 @@ -import someLimit from './someLimit'; -import doLimit from './internal/doLimit'; +import createTester from './internal/createTester'; +import eachOfSeries from './eachOfSeries'; +import awaitify from './internal/awaitify'; /** * The same as [`some`]{@link module:Collections.some} but runs only a single async operation at a time. @@ -20,5 +21,9 @@ import doLimit from './internal/doLimit'; * iteratee returns `true`, or 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(someLimit, 1); +function someSeries(coll, iteratee, callback) { + return createTester(Boolean, res => res)(eachOfSeries, coll, iteratee, callback) +} +export default awaitify(someSeries, 3); diff --git a/lib/sortBy.js b/lib/sortBy.js index ba2a804..ec724b6 100644 --- a/lib/sortBy.js +++ b/lib/sortBy.js @@ -1,5 +1,6 @@ import map from './map'; import wrapAsync from './internal/wrapAsync'; +import awaitify from './internal/awaitify' /** * Sorts a list by the results of running each `coll` value through an async @@ -20,6 +21,7 @@ import wrapAsync from './internal/wrapAsync'; * `iteratee` functions have finished, or an error occurs. Results is the items * from the original `coll` sorted by the values returned by the `iteratee` * calls. Invoked with (err, results). + * @returns {Promise} a promise, if no callback passed * @example * * async.sortBy(['file1','file2','file3'], function(file, callback) { @@ -48,9 +50,9 @@ import wrapAsync from './internal/wrapAsync'; * // result callback * }); */ -export default function sortBy (coll, iteratee, callback) { +function sortBy (coll, iteratee, callback) { var _iteratee = wrapAsync(iteratee); - map(coll, (x, iterCb) => { + return map(coll, (x, iterCb) => { _iteratee(x, (err, criteria) => { if (err) return iterCb(err); iterCb(null, {value: x, criteria}); @@ -65,3 +67,4 @@ export default function sortBy (coll, iteratee, callback) { return a < b ? -1 : a > b ? 1 : 0; } } +export default awaitify(sortBy, 3) diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index f9a64e3..156df08 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -253,4 +253,31 @@ module.exports = function () { await async.rejectLimit(inputObj, 1, async (...args) => { calls.push(args) }); expect(calls).to.eql([[1], [2], [3]]) }); + + it('should return a Promise: some', async () => { + expect (async.some.name).to.contain('some') + const calls = [] + await async.some(input, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: someSeries', async () => { + expect (async.someSeries.name).to.contain('someSeries') + const calls = [] + await async.someSeries(input, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: someLimit', async () => { + expect (async.someLimit.name).to.contain('someLimit') + const calls = [] + await async.someLimit(input, 1, async (...args) => { calls.push(args); return args[0] === 3 }); + expect(calls).to.eql([[1], [2], [3]]) + }); + + + it('should return a Promise: sortBy', async () => { + expect (async.sortBy.name).to.contain('sortBy') + const calls = [] + await async.sortBy(input, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); }; |
