diff options
-rw-r--r-- | lib/reduce.js | 8 | ||||
-rw-r--r-- | lib/reduceRight.js | 3 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 14 |
3 files changed, 22 insertions, 3 deletions
diff --git a/lib/reduce.js b/lib/reduce.js index 1a6c036..b7434f0 100644 --- a/lib/reduce.js +++ b/lib/reduce.js @@ -2,6 +2,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 +33,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 +45,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) { +function reduce(coll, memo, iteratee, callback) { callback = once(callback || noop); 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) + diff --git a/lib/reduceRight.js b/lib/reduceRight.js index dd56a88..994eaf9 100644 --- a/lib/reduceRight.js +++ b/lib/reduceRight.js @@ -21,8 +21,9 @@ import reduce from './reduce'; * @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 */ export default function reduceRight (array, memo, iteratee, callback) { var reversed = [...array].reverse(); - reduce(reversed, memo, iteratee, callback); + return reduce(reversed, memo, iteratee, callback); } diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 1656894..b51a23b 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -220,4 +220,18 @@ module.exports = function () { await async.mapValuesLimit(inputObj, 1, async (...args) => { calls.push(args) }); expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']]) }); + + + it('should return a Promise: reduce', async () => { + expect (async.reduce.name).to.contain('reduce') + const calls = [] + await async.reduce(input, 1, async (...args) => calls.push(args)); + expect(calls).to.eql([[1, 1], [1, 2], [2, 3]]) + }); + it('should return a Promise: reduceRight', async () => { + expect (async.reduceRight.name).to.contain('reduceRight') + const calls = [] + await async.reduceRight(input, 1, async (...args) => calls.push(args)); + expect(calls).to.eql([[1, 3], [1, 2], [2, 1]]) + }); }; |