summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-09-02 17:31:40 -0700
committerAlexander Early <alexander.early@gmail.com>2018-09-02 17:31:40 -0700
commite5f01b89b95d781e63b988595727d143b494a566 (patch)
treefad05cabba54c5ec7525557b93ab1ddd398ea439
parentab7ccd148fcbbd0e56acca628a5c78b419852410 (diff)
downloadasync-e5f01b89b95d781e63b988595727d143b494a566.tar.gz
awaitable map/mapValues
-rw-r--r--lib/map.js11
-rw-r--r--lib/mapLimit.js11
-rw-r--r--lib/mapSeries.js11
-rw-r--r--lib/mapValues.js8
-rw-r--r--lib/mapValuesLimit.js11
-rw-r--r--lib/mapValuesSeries.js6
-rw-r--r--test/es2017/awaitableFunctions.js38
7 files changed, 77 insertions, 19 deletions
diff --git a/lib/map.js b/lib/map.js
index 2512c32..c32bd2d 100644
--- a/lib/map.js
+++ b/lib/map.js
@@ -1,5 +1,6 @@
-import doParallel from './internal/doParallel';
-import map from './internal/map';
+import _map from './internal/map';
+import eachOf from './eachOf'
+import awaitify from './internal/awaitify'
/**
* Produces a new collection of values by mapping each value in `coll` through
@@ -31,10 +32,14 @@ import map from './internal/map';
* @param {Function} [callback] - A callback which is called when all `iteratee`
* functions have finished, or an error occurs. Results is an Array of the
* transformed items from the `coll`. Invoked with (err, results).
+ * @returns {Promise} a promise, if no callback is passed
* @example
*
* async.map(['file1','file2','file3'], fs.stat, function(err, results) {
* // results is now an array of stats for each file
* });
*/
-export default doParallel(map);
+function map (coll, iteratee, callback) {
+ return _map(eachOf, coll, iteratee, callback)
+}
+export default awaitify(map, 3);
diff --git a/lib/mapLimit.js b/lib/mapLimit.js
index 905edc6..63fade2 100644
--- a/lib/mapLimit.js
+++ b/lib/mapLimit.js
@@ -1,5 +1,6 @@
-import doParallelLimit from './internal/doParallelLimit';
-import map from './internal/map';
+import _map from './internal/map';
+import eachOfLimit from './internal/eachOfLimit'
+import awaitify from './internal/awaitify'
/**
* The same as [`map`]{@link module:Collections.map} but runs a maximum of `limit` async operations at a time.
@@ -19,5 +20,9 @@ import map from './internal/map';
* @param {Function} [callback] - A callback which is called when all `iteratee`
* functions have finished, or an error occurs. Results is an array of the
* transformed items from the `coll`. Invoked with (err, results).
+ * @returns {Promise} a promise, if no callback is passed
*/
-export default doParallelLimit(map);
+function mapLimit (coll, limit, iteratee, callback) {
+ return _map(eachOfLimit(limit), coll, iteratee, callback)
+}
+export default awaitify(mapLimit, 4);
diff --git a/lib/mapSeries.js b/lib/mapSeries.js
index 96b5f0a..55c6831 100644
--- a/lib/mapSeries.js
+++ b/lib/mapSeries.js
@@ -1,5 +1,6 @@
-import mapLimit from './mapLimit';
-import doLimit from './internal/doLimit';
+import _map from './internal/map';
+import eachOfSeries from './eachOfSeries'
+import awaitify from './internal/awaitify'
/**
* The same as [`map`]{@link module:Collections.map} but runs only a single async operation at a time.
@@ -18,5 +19,9 @@ import doLimit from './internal/doLimit';
* @param {Function} [callback] - A callback which is called when all `iteratee`
* functions have finished, or an error occurs. Results is an array of the
* transformed items from the `coll`. Invoked with (err, results).
+ * @returns {Promise} a promise, if no callback is passed
*/
-export default doLimit(mapLimit, 1);
+function mapSeries (coll, iteratee, callback) {
+ return _map(eachOfSeries, coll, iteratee, callback)
+}
+export default awaitify(mapSeries, 3);
diff --git a/lib/mapValues.js b/lib/mapValues.js
index d6a4ad2..79edfd4 100644
--- a/lib/mapValues.js
+++ b/lib/mapValues.js
@@ -1,6 +1,4 @@
import mapValuesLimit from './mapValuesLimit';
-import doLimit from './internal/doLimit';
-
/**
* A relative of [`map`]{@link module:Collections.map}, designed for use with objects.
@@ -29,6 +27,7 @@ import doLimit from './internal/doLimit';
* functions have finished, or an error occurs. `result` is a new object consisting
* of each key from `obj`, with each transformed value on the right-hand side.
* Invoked with (err, result).
+ * @returns {Promise} a promise, if no callback is passed
* @example
*
* async.mapValues({
@@ -46,5 +45,6 @@ import doLimit from './internal/doLimit';
* // }
* });
*/
-
-export default doLimit(mapValuesLimit, Infinity);
+export default function mapValues(obj, iteratee, callback) {
+ return mapValuesLimit(obj, Infinity, iteratee, callback)
+}
diff --git a/lib/mapValuesLimit.js b/lib/mapValuesLimit.js
index 2267aaf..b88a1d8 100644
--- a/lib/mapValuesLimit.js
+++ b/lib/mapValuesLimit.js
@@ -1,5 +1,5 @@
-import eachOfLimit from './eachOfLimit';
-
+import eachOfLimit from './internal/eachOfLimit'
+import awaitify from './internal/awaitify'
import noop from './internal/noop';
import once from './internal/once';
import wrapAsync from './internal/wrapAsync';
@@ -24,12 +24,13 @@ import wrapAsync from './internal/wrapAsync';
* functions have finished, or an error occurs. `result` is a new object consisting
* of each key from `obj`, with each transformed value on the right-hand side.
* Invoked with (err, result).
+ * @returns {Promise} a promise, if no callback is passed
*/
-export default function mapValuesLimit(obj, limit, iteratee, callback) {
+function mapValuesLimit(obj, limit, iteratee, callback) {
callback = once(callback || noop);
var newObj = {};
var _iteratee = wrapAsync(iteratee)
- eachOfLimit(obj, limit, (val, key, next) => {
+ return eachOfLimit(limit)(obj, (val, key, next) => {
_iteratee(val, key, (err, result) => {
if (err) return next(err);
newObj[key] = result;
@@ -37,3 +38,5 @@ export default function mapValuesLimit(obj, limit, iteratee, callback) {
});
}, err => callback(err, newObj));
}
+
+export default awaitify(mapValuesLimit, 4)
diff --git a/lib/mapValuesSeries.js b/lib/mapValuesSeries.js
index ab0b8f8..59e4b1a 100644
--- a/lib/mapValuesSeries.js
+++ b/lib/mapValuesSeries.js
@@ -1,5 +1,4 @@
import mapValuesLimit from './mapValuesLimit';
-import doLimit from './internal/doLimit';
/**
* The same as [`mapValues`]{@link module:Collections.mapValues} but runs only a single async operation at a time.
@@ -19,5 +18,8 @@ import doLimit from './internal/doLimit';
* functions have finished, or an error occurs. `result` is a new object consisting
* of each key from `obj`, with each transformed value on the right-hand side.
* Invoked with (err, result).
+ * @returns {Promise} a promise, if no callback is passed
*/
-export default doLimit(mapValuesLimit, 1);
+export default function mapValuesSeries(obj, iteratee, callback) {
+ return mapValuesLimit(obj, 1, iteratee, callback)
+}
diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js
index 31d158b..1656894 100644
--- a/test/es2017/awaitableFunctions.js
+++ b/test/es2017/awaitableFunctions.js
@@ -182,4 +182,42 @@ module.exports = function () {
await async.groupByLimit(inputObj, 1, async (...args) => { calls.push(args) });
expect(calls).to.eql([[1], [2], [3]])
});
+
+ it('should return a Promise: map', async () => {
+ expect (async.map.name).to.contain('map')
+ const calls = []
+ await async.map(inputObj, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
+ it('should return a Promise: mapSeries', async () => {
+ expect (async.mapSeries.name).to.contain('mapSeries')
+ const calls = []
+ await async.mapSeries(inputObj, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
+ it('should return a Promise: mapLimit', async () => {
+ expect (async.mapLimit.name).to.contain('mapLimit')
+ const calls = []
+ await async.mapLimit(inputObj, 1, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1], [2], [3]])
+ });
+
+ it('should return a Promise: mapValues', async () => {
+ expect (async.mapValues.name).to.contain('mapValues')
+ const calls = []
+ await async.mapValues(inputObj, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])
+ });
+ it('should return a Promise: mapValuesSeries', async () => {
+ expect (async.mapValuesSeries.name).to.contain('mapValuesSeries')
+ const calls = []
+ await async.mapValuesSeries(inputObj, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])
+ });
+ it('should return a Promise: mapValuesLimit', async () => {
+ expect (async.mapValuesLimit.name).to.contain('mapValuesLimit')
+ const calls = []
+ await async.mapValuesLimit(inputObj, 1, async (...args) => { calls.push(args) });
+ expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']])
+ });
};