summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-09-02 16:55:12 -0700
committerAlexander Early <alexander.early@gmail.com>2018-09-02 16:55:12 -0700
commit6e07f6c5f4f2abe844d47651a03a449f864a5190 (patch)
treec33b49cc5cda72046b21757d466451ce4316338d
parent0c7493423b60246f987d106d1f040e83de5196f7 (diff)
downloadasync-6e07f6c5f4f2abe844d47651a03a449f864a5190.tar.gz
awaitable detect
-rw-r--r--lib/detect.js9
-rw-r--r--lib/detectLimit.js9
-rw-r--r--lib/detectSeries.js12
-rw-r--r--lib/eachOfSeries.js2
-rw-r--r--lib/eachSeries.js2
-rw-r--r--lib/internal/awaitify.js9
-rw-r--r--lib/internal/createTester.js4
-rw-r--r--test/es2017/awaitableFunctions.js19
8 files changed, 52 insertions, 14 deletions
diff --git a/lib/detect.js b/lib/detect.js
index 3e28f0e..4a6746a 100644
--- a/lib/detect.js
+++ b/lib/detect.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 the first value in `coll` that passes an async truth test. The
@@ -26,6 +27,7 @@ import doParallel from './internal/doParallel';
* Result will be the first item in the array that passes the truth test
* (iteratee) or the value `undefined` if none passed. Invoked with
* (err, result).
+ * @returns A Promise, if no callback is passed
* @example
*
* async.detect(['file1','file2','file3'], function(filePath, callback) {
@@ -36,4 +38,7 @@ import doParallel from './internal/doParallel';
* // result now equals the first file in the list that exists
* });
*/
-export default doParallel(createTester(bool => bool, (res, item) => item));
+function detect(coll, iteratee, callback) {
+ return createTester(bool => bool, (res, item) => item)(eachOf, coll, iteratee, callback)
+}
+export default awaitify(detect, 3)
diff --git a/lib/detectLimit.js b/lib/detectLimit.js
index a0f9d89..7630d6d 100644
--- a/lib/detectLimit.js
+++ b/lib/detectLimit.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 [`detect`]{@link module:Collections.detect} but runs a maximum of `limit` async operations at a
@@ -22,5 +23,9 @@ import doParallelLimit from './internal/doParallelLimit';
* Result will be the first item in the array that passes the truth test
* (iteratee) or the value `undefined` if none passed. Invoked with
* (err, result).
+ * @returns a Promise if no callback is passed
*/
-export default doParallelLimit(createTester(bool => bool, (res, item) => item));
+function detectLimit(coll, limit, iteratee, callback) {
+ return createTester(bool => bool, (res, item) => item)(eachOfLimit(limit), coll, iteratee, callback)
+}
+export default awaitify(detectLimit, 4)
diff --git a/lib/detectSeries.js b/lib/detectSeries.js
index f563eae..e85c4f8 100644
--- a/lib/detectSeries.js
+++ b/lib/detectSeries.js
@@ -1,5 +1,6 @@
-import detectLimit from './detectLimit';
-import doLimit from './internal/doLimit';
+import createTester from './internal/createTester'
+import eachOfLimit from './internal/eachOfLimit'
+import awaitify from './internal/awaitify'
/**
* The same as [`detect`]{@link module:Collections.detect} but runs only a single async operation at a time.
@@ -20,5 +21,10 @@ import doLimit from './internal/doLimit';
* Result will be the first item in the array that passes the truth test
* (iteratee) or the value `undefined` if none passed. Invoked with
* (err, result).
+ * @returns a Promise if no callback is passed
*/
-export default doLimit(detectLimit, 1);
+function detectSeries(coll, iteratee, callback) {
+ return createTester(bool => bool, (res, item) => item)(eachOfLimit(1), coll, iteratee, callback)
+}
+
+export default awaitify(detectSeries, 3)
diff --git a/lib/eachOfSeries.js b/lib/eachOfSeries.js
index d28e402..c48a66b 100644
--- a/lib/eachOfSeries.js
+++ b/lib/eachOfSeries.js
@@ -22,4 +22,4 @@ import awaitify from './internal/awaitify'
function eachOfSeries(coll, iteratee, callback) {
return eachOfLimit(coll, 1, iteratee, callback)
}
-export default awaitify(eachOfSeries);
+export default awaitify(eachOfSeries, 3);
diff --git a/lib/eachSeries.js b/lib/eachSeries.js
index 989762e..b4a86ba 100644
--- a/lib/eachSeries.js
+++ b/lib/eachSeries.js
@@ -24,4 +24,4 @@ import awaitify from './internal/awaitify'
function eachSeries(coll, iteratee, callback) {
return eachLimit(coll, 1, iteratee, callback)
}
-export default awaitify(eachSeries);
+export default awaitify(eachSeries, 3);
diff --git a/lib/internal/awaitify.js b/lib/internal/awaitify.js
index 488398d..cad271b 100644
--- a/lib/internal/awaitify.js
+++ b/lib/internal/awaitify.js
@@ -1,16 +1,17 @@
// conditionally promisify a function.
// only return a promise if a callback is omitted
export default function awaitify (asyncFn, arity) {
- const awaitable = function (...args) {
- if (args.length === arity || typeof args[arity - 1] === 'function') {
+ if (arity == null) throw new Error('arity is undefined')
+ function awaitable (...args) {
+ if (typeof args[arity - 1] === 'function') {
return asyncFn.apply(this, args)
}
return new Promise((resolve, reject) => {
- args.push((err, ...cbArgs) => {
+ args[arity - 1] = (err, ...cbArgs) => {
if (err) return reject(err)
resolve(cbArgs.length > 1 ? cbArgs : cbArgs[0])
- })
+ }
asyncFn.apply(this, args)
})
}
diff --git a/lib/internal/createTester.js b/lib/internal/createTester.js
index 4a5d39f..909679d 100644
--- a/lib/internal/createTester.js
+++ b/lib/internal/createTester.js
@@ -1,11 +1,13 @@
import noop from './noop';
import breakLoop from './breakLoop';
+import wrapAsync from './wrapAsync'
export default function _createTester(check, getResult) {
- return (eachfn, arr, iteratee, cb) => {
+ return (eachfn, arr, _iteratee, cb) => {
cb = cb || noop;
var testPassed = false;
var testResult;
+ const iteratee = wrapAsync(_iteratee)
eachfn(arr, (value, _, callback) => {
iteratee(value, (err, result) => {
if (err) return callback(err)
diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js
index e551609..00748ce 100644
--- a/test/es2017/awaitableFunctions.js
+++ b/test/es2017/awaitableFunctions.js
@@ -106,4 +106,23 @@ module.exports = function () {
await async.concatLimit(inputObj, 1, async (...args) => { calls.push(args) });
expect(calls).to.eql([[1], [2], [3]])
});
+
+ it('should return a Promise: detect', async () => {
+ expect (async.detect.name).to.contain('detect')
+ const calls = []
+ await async.detect(input, async (...args) => { calls.push(args); return args[0] === 3 });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
+ it('should return a Promise: detectSeries', async () => {
+ expect (async.detectSeries.name).to.contain('detectSeries')
+ const calls = []
+ await async.detectSeries(input, async (...args) => { calls.push(args); return args[0] === 3 });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
+ it('should return a Promise: detectLimit', async () => {
+ expect (async.detectLimit.name).to.contain('detectLimit')
+ const calls = []
+ await async.detectLimit(input, 1, async (...args) => { calls.push(args); return args[0] === 3 });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
};