summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/eachOf.js13
-rw-r--r--lib/eachOfSeries.js6
-rw-r--r--lib/eachSeries.js6
-rw-r--r--lib/internal/awaitify.js6
-rw-r--r--test/es2017/awaitableFunctions.js14
5 files changed, 34 insertions, 11 deletions
diff --git a/lib/eachOf.js b/lib/eachOf.js
index f3bda13..bb12235 100644
--- a/lib/eachOf.js
+++ b/lib/eachOf.js
@@ -1,7 +1,6 @@
import isArrayLike from './internal/isArrayLike';
import breakLoop from './internal/breakLoop';
import eachOfLimit from './eachOfLimit';
-import doLimit from './internal/doLimit';
import noop from './internal/noop';
import once from './internal/once';
import onlyOnce from './internal/onlyOnce';
@@ -37,7 +36,9 @@ function eachOfArrayLike(coll, iteratee, callback) {
}
// a generic version of eachOf which can handle array, object, and iterator cases.
-var eachOfGeneric = doLimit(eachOfLimit, Infinity);
+function eachOfGeneric (coll, iteratee, callback) {
+ return eachOfLimit(coll, Infinity, iteratee, callback);
+}
/**
* Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
@@ -79,7 +80,9 @@ var eachOfGeneric = doLimit(eachOfLimit, Infinity);
* doSomethingWith(configs);
* });
*/
-export default awaitify((coll, iteratee, callback) => {
+function eachOf(coll, iteratee, callback) {
var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
- eachOfImplementation(coll, wrapAsync(iteratee), callback);
-}, 3)
+ return eachOfImplementation(coll, wrapAsync(iteratee), callback);
+}
+
+export default awaitify(eachOf, 3)
diff --git a/lib/eachOfSeries.js b/lib/eachOfSeries.js
index fbb4e11..d28e402 100644
--- a/lib/eachOfSeries.js
+++ b/lib/eachOfSeries.js
@@ -1,5 +1,4 @@
import eachOfLimit from './eachOfLimit';
-import doLimit from './internal/doLimit';
import awaitify from './internal/awaitify'
/**
@@ -20,4 +19,7 @@ import awaitify from './internal/awaitify'
* functions have finished, or an error occurs. Invoked with (err).
* @returns {Promise} a promise, if a callback is omitted
*/
-export default awaitify(doLimit(eachOfLimit, 1), 3);
+function eachOfSeries(coll, iteratee, callback) {
+ return eachOfLimit(coll, 1, iteratee, callback)
+}
+export default awaitify(eachOfSeries);
diff --git a/lib/eachSeries.js b/lib/eachSeries.js
index 5e499b1..989762e 100644
--- a/lib/eachSeries.js
+++ b/lib/eachSeries.js
@@ -1,5 +1,4 @@
import eachLimit from './eachLimit';
-import doLimit from './internal/doLimit';
import awaitify from './internal/awaitify'
/**
@@ -22,4 +21,7 @@ import awaitify from './internal/awaitify'
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
* @returns {Promise} a promise, if a callback is omitted
*/
-export default awaitify(doLimit(eachLimit, 1), 3);
+function eachSeries(coll, iteratee, callback) {
+ return eachLimit(coll, 1, iteratee, callback)
+}
+export default awaitify(eachSeries);
diff --git a/lib/internal/awaitify.js b/lib/internal/awaitify.js
index a65bbcb..488398d 100644
--- a/lib/internal/awaitify.js
+++ b/lib/internal/awaitify.js
@@ -1,7 +1,7 @@
// conditionally promisify a function.
// only return a promise if a callback is omitted
export default function awaitify (asyncFn, arity) {
- function awaitable(...args) {
+ const awaitable = function (...args) {
if (args.length === arity || typeof args[arity - 1] === 'function') {
return asyncFn.apply(this, args)
}
@@ -15,7 +15,9 @@ export default function awaitify (asyncFn, arity) {
})
}
- awaitable.displayName = `awaitable(${asyncFn.name})`
+ Object.defineProperty(awaitable, 'name', {
+ value: `awaitable(${asyncFn.name})`
+ })
return awaitable
}
diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js
index fc866fa..6b375c3 100644
--- a/test/es2017/awaitableFunctions.js
+++ b/test/es2017/awaitableFunctions.js
@@ -29,6 +29,14 @@ module.exports = function () {
});
it('should throw as expected (callback)', async () => {
+ let thrown
+ await async.each(input, (val) => {
+ throw new Error(val)
+ }).catch(e => {thrown = e})
+ expect(thrown).to.be.an('error')
+ })
+
+ it('should throw as expected (callback, try/catch)', async () => {
try {
await async.each(input, (val, cb) => { cb(new Error(val)) });
} catch (e) {
@@ -42,33 +50,39 @@ module.exports = function () {
*/
it('should return a Promise: each', async () => {
+ expect (async.each.name).to.contain('each')
const calls = []
await async.each(input, async val => { calls.push(val) });
expect(calls).to.eql([1, 2, 3])
expect(async.each(input, asyncIdentity) instanceof Promise).to.equal(true)
});
it('should return a Promise: eachSeries', async () => {
+ expect (async.eachSeries.name).to.contain('eachSeries')
const calls = []
await async.eachSeries(input, async val => { calls.push(val) });
expect(calls).to.eql([1, 2, 3])
});
it('should return a Promise: eachLimit', async () => {
+ expect (async.eachLimit.name).to.contain('eachLimit')
const calls = []
await async.eachLimit(input, 1, async val => { calls.push(val) });
expect(calls).to.eql([1, 2, 3])
});
it('should return a Promise: eachOf', async () => {
+ expect (async.eachOf.name).to.contain('eachOf')
const calls = []
await async.eachOf(inputObj, async (...args) => { calls.push(args) });
expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])
});
it('should return a Promise: eachOfSeries', async () => {
+ expect (async.eachOfSeries.name).to.contain('eachOfSeries')
const calls = []
await async.eachOfSeries(inputObj, async (...args) => { calls.push(args) });
expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])
});
it('should return a Promise: eachOfLimit', async () => {
+ expect (async.eachOfLimit.name).to.contain('eachOfLimit')
const calls = []
await async.eachOfLimit(inputObj, 1, async (...args) => { calls.push(args) });
expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])