root / HServer / 00.Server / 00.Program / node_modules / async / eachOf.js
이력 | 보기 | 이력해설 | 다운로드 (3.21 KB)
1 | 39 | HKM | 'use strict';
|
---|---|---|---|
2 | |||
3 | Object.defineProperty(exports, "__esModule", {
|
||
4 | value: true |
||
5 | }); |
||
6 | |||
7 | exports.default = function (coll, iteratee, callback) { |
||
8 | var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric; |
||
9 | eachOfImplementation(coll, iteratee, callback); |
||
10 | }; |
||
11 | |||
12 | var _isArrayLike = require('lodash/isArrayLike'); |
||
13 | |||
14 | var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
|
||
15 | |||
16 | var _eachOfLimit = require('./eachOfLimit'); |
||
17 | |||
18 | var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
|
||
19 | |||
20 | var _doLimit = require('./internal/doLimit'); |
||
21 | |||
22 | var _doLimit2 = _interopRequireDefault(_doLimit);
|
||
23 | |||
24 | var _noop = require('lodash/noop'); |
||
25 | |||
26 | var _noop2 = _interopRequireDefault(_noop);
|
||
27 | |||
28 | var _once = require('./internal/once'); |
||
29 | |||
30 | var _once2 = _interopRequireDefault(_once);
|
||
31 | |||
32 | var _onlyOnce = require('./internal/onlyOnce'); |
||
33 | |||
34 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
||
35 | |||
36 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
||
37 | |||
38 | // eachOf implementation optimized for array-likes
|
||
39 | function eachOfArrayLike(coll, iteratee, callback) { |
||
40 | callback = (0, _once2.default)(callback || _noop2.default); |
||
41 | var index = 0, |
||
42 | completed = 0,
|
||
43 | length = coll.length; |
||
44 | if (length === 0) { |
||
45 | callback(null);
|
||
46 | } |
||
47 | |||
48 | function iteratorCallback(err) { |
||
49 | if (err) {
|
||
50 | callback(err); |
||
51 | } else if (++completed === length) { |
||
52 | callback(null);
|
||
53 | } |
||
54 | } |
||
55 | |||
56 | for (; index < length; index++) {
|
||
57 | iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback)); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // a generic version of eachOf which can handle array, object, and iterator cases.
|
||
62 | var eachOfGeneric = (0, _doLimit2.default)(_eachOfLimit2.default, Infinity); |
||
63 | |||
64 | /**
|
||
65 | * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
||
66 | * to the iteratee.
|
||
67 | *
|
||
68 | * @name eachOf
|
||
69 | * @static
|
||
70 | * @memberOf module:Collections
|
||
71 | * @method
|
||
72 | * @alias forEachOf
|
||
73 | * @category Collection
|
||
74 | * @see [async.each]{@link module:Collections.each}
|
||
75 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
||
76 | * @param {Function} iteratee - A function to apply to each
|
||
77 | * item in `coll`. The `key` is the item's key, or index in the case of an
|
||
78 | * array. The iteratee is passed a `callback(err)` which must be called once it
|
||
79 | * has completed. If no error has occurred, the callback should be run without
|
||
80 | * arguments or with an explicit `null` argument. Invoked with
|
||
81 | * (item, key, callback).
|
||
82 | * @param {Function} [callback] - A callback which is called when all
|
||
83 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
||
84 | * @example
|
||
85 | *
|
||
86 | * var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
|
||
87 | * var configs = {};
|
||
88 | *
|
||
89 | * async.forEachOf(obj, function (value, key, callback) {
|
||
90 | * fs.readFile(__dirname + value, "utf8", function (err, data) {
|
||
91 | * if (err) return callback(err);
|
||
92 | * try {
|
||
93 | * configs[key] = JSON.parse(data);
|
||
94 | * } catch (e) {
|
||
95 | * return callback(e);
|
||
96 | * }
|
||
97 | * callback();
|
||
98 | * });
|
||
99 | * }, function (err) {
|
||
100 | * if (err) console.error(err.message);
|
||
101 | * // configs is now a map of JSON data
|
||
102 | * doSomethingWith(configs);
|
||
103 | * });
|
||
104 | */
|
||
105 | module.exports = exports['default']; |