root / HServer / 00.Server / 00.Program / node_modules / async / reduce.js
이력 | 보기 | 이력해설 | 다운로드 (2.53 KB)
| 1 | 39 | HKM | 'use strict';
|
|---|---|---|---|
| 2 | |||
| 3 | Object.defineProperty(exports, "__esModule", {
|
||
| 4 | value: true |
||
| 5 | }); |
||
| 6 | exports.default = reduce;
|
||
| 7 | |||
| 8 | var _eachOfSeries = require('./eachOfSeries'); |
||
| 9 | |||
| 10 | var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
|
||
| 11 | |||
| 12 | var _noop = require('lodash/noop'); |
||
| 13 | |||
| 14 | var _noop2 = _interopRequireDefault(_noop);
|
||
| 15 | |||
| 16 | var _once = require('./internal/once'); |
||
| 17 | |||
| 18 | var _once2 = _interopRequireDefault(_once);
|
||
| 19 | |||
| 20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
||
| 21 | |||
| 22 | /**
|
||
| 23 | * Reduces `coll` into a single value using an async `iteratee` to return each
|
||
| 24 | * successive step. `memo` is the initial state of the reduction. This function
|
||
| 25 | * only operates in series.
|
||
| 26 | *
|
||
| 27 | * For performance reasons, it may make sense to split a call to this function
|
||
| 28 | * into a parallel map, and then use the normal `Array.prototype.reduce` on the
|
||
| 29 | * results. This function is for situations where each step in the reduction
|
||
| 30 | * needs to be async; if you can get the data before reducing it, then it's
|
||
| 31 | * probably a good idea to do so.
|
||
| 32 | *
|
||
| 33 | * @name reduce
|
||
| 34 | * @static
|
||
| 35 | * @memberOf module:Collections
|
||
| 36 | * @method
|
||
| 37 | * @alias inject
|
||
| 38 | * @alias foldl
|
||
| 39 | * @category Collection
|
||
| 40 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
||
| 41 | * @param {*} memo - The initial state of the reduction.
|
||
| 42 | * @param {Function} iteratee - A function applied to each item in the
|
||
| 43 | * array to produce the next step in the reduction. The `iteratee` is passed a
|
||
| 44 | * `callback(err, reduction)` which accepts an optional error as its first
|
||
| 45 | * argument, and the state of the reduction as the second. If an error is
|
||
| 46 | * passed to the callback, the reduction is stopped and the main `callback` is
|
||
| 47 | * immediately called with the error. Invoked with (memo, item, callback).
|
||
| 48 | * @param {Function} [callback] - A callback which is called after all the
|
||
| 49 | * `iteratee` functions have finished. Result is the reduced value. Invoked with
|
||
| 50 | * (err, result).
|
||
| 51 | * @example
|
||
| 52 | *
|
||
| 53 | * async.reduce([1,2,3], 0, function(memo, item, callback) {
|
||
| 54 | * // pointless async:
|
||
| 55 | * process.nextTick(function() {
|
||
| 56 | * callback(null, memo + item)
|
||
| 57 | * });
|
||
| 58 | * }, function(err, result) {
|
||
| 59 | * // result is now equal to the last value of memo, which is 6
|
||
| 60 | * });
|
||
| 61 | */
|
||
| 62 | function reduce(coll, memo, iteratee, callback) { |
||
| 63 | callback = (0, _once2.default)(callback || _noop2.default); |
||
| 64 | (0, _eachOfSeries2.default)(coll, function (x, i, callback) { |
||
| 65 | iteratee(memo, x, function (err, v) {
|
||
| 66 | memo = v; |
||
| 67 | callback(err); |
||
| 68 | }); |
||
| 69 | }, function (err) {
|
||
| 70 | callback(err, memo); |
||
| 71 | }); |
||
| 72 | } |
||
| 73 | module.exports = exports['default']; |