summaryrefslogtreecommitdiff
path: root/lib/reduce.js
diff options
context:
space:
mode:
authorAlex Early <alexander.early@gmail.com>2018-09-30 17:00:10 -0700
committerGitHub <noreply@github.com>2018-09-30 17:00:10 -0700
commit8aecf108b3922bc5211036706a0f6f75e02bd42b (patch)
tree0f7b6bee315231ef4aefdfbee154822921de231f /lib/reduce.js
parentdf41256f49c9bb3126e035c95aca7860329b6acf (diff)
downloadasync-8aecf108b3922bc5211036706a0f6f75e02bd42b.tar.gz
feat: await-able Async methods (#1572)
* make each and family awaitable * dont pretend they're AsyncFunctions * check errors * ensure function name is preserved somehow * awaitable concat * awaitable detect * awaitable every/filter * awaitable groupBy * awaitable map/mapValues * awaitable reduce * awaitable reject * awaitable some * awaitable transform * awaitable times * awaitable auto * awaitable compose/seq * awaitable whilst/until (lol) * awaitable forever * awaitable parallel/race * awaitable retry * awaitable series (lol) * awaitable tryEach * awaitable waterfall (lol) * lint * cleanup, remove noop and unused internal functions
Diffstat (limited to 'lib/reduce.js')
-rw-r--r--lib/reduce.js11
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/reduce.js b/lib/reduce.js
index 1a6c036..571bc5a 100644
--- a/lib/reduce.js
+++ b/lib/reduce.js
@@ -1,7 +1,7 @@
import eachOfSeries from './eachOfSeries';
-import noop from './internal/noop';
import once from './internal/once';
import wrapAsync from './internal/wrapAsync';
+import awaitify from './internal/awaitify'
/**
* Reduces `coll` into a single value using an async `iteratee` to return each
@@ -32,6 +32,7 @@ import wrapAsync from './internal/wrapAsync';
* @param {Function} [callback] - A callback which is called after all the
* `iteratee` functions have finished. Result is the reduced value. Invoked with
* (err, result).
+ * @returns {Promise} a promise, if no callback is passed
* @example
*
* async.reduce([1,2,3], 0, function(memo, item, callback) {
@@ -43,13 +44,15 @@ import wrapAsync from './internal/wrapAsync';
* // result is now equal to the last value of memo, which is 6
* });
*/
-export default function reduce(coll, memo, iteratee, callback) {
- callback = once(callback || noop);
+function reduce(coll, memo, iteratee, callback) {
+ callback = once(callback);
var _iteratee = wrapAsync(iteratee);
- eachOfSeries(coll, (x, i, iterCb) => {
+ return eachOfSeries(coll, (x, i, iterCb) => {
_iteratee(memo, x, (err, v) => {
memo = v;
iterCb(err);
});
}, err => callback(err, memo));
}
+export default awaitify(reduce, 4)
+