UNPKG

4.03 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _eachOf = require('./eachOf.js');
8
9var _eachOf2 = _interopRequireDefault(_eachOf);
10
11var _withoutIndex = require('./internal/withoutIndex.js');
12
13var _withoutIndex2 = _interopRequireDefault(_withoutIndex);
14
15var _wrapAsync = require('./internal/wrapAsync.js');
16
17var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
18
19var _awaitify = require('./internal/awaitify.js');
20
21var _awaitify2 = _interopRequireDefault(_awaitify);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25/**
26 * Applies the function `iteratee` to each item in `coll`, in parallel.
27 * The `iteratee` is called with an item from the list, and a callback for when
28 * it has finished. If the `iteratee` passes an error to its `callback`, the
29 * main `callback` (for the `each` function) is immediately called with the
30 * error.
31 *
32 * Note, that since this function applies `iteratee` to each item in parallel,
33 * there is no guarantee that the iteratee functions will complete in order.
34 *
35 * @name each
36 * @static
37 * @memberOf module:Collections
38 * @method
39 * @alias forEach
40 * @category Collection
41 * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
42 * @param {AsyncFunction} iteratee - An async function to apply to
43 * each item in `coll`. Invoked with (item, callback).
44 * The array index is not passed to the iteratee.
45 * If you need the index, use `eachOf`.
46 * @param {Function} [callback] - A callback which is called when all
47 * `iteratee` functions have finished, or an error occurs. Invoked with (err).
48 * @returns {Promise} a promise, if a callback is omitted
49 * @example
50 *
51 * // dir1 is a directory that contains file1.txt, file2.txt
52 * // dir2 is a directory that contains file3.txt, file4.txt
53 * // dir3 is a directory that contains file5.txt
54 * // dir4 does not exist
55 *
56 * const fileList = [ 'dir1/file2.txt', 'dir2/file3.txt', 'dir/file5.txt'];
57 * const withMissingFileList = ['dir1/file1.txt', 'dir4/file2.txt'];
58 *
59 * // asynchronous function that deletes a file
60 * const deleteFile = function(file, callback) {
61 * fs.unlink(file, callback);
62 * };
63 *
64 * // Using callbacks
65 * async.each(fileList, deleteFile, function(err) {
66 * if( err ) {
67 * console.log(err);
68 * } else {
69 * console.log('All files have been deleted successfully');
70 * }
71 * });
72 *
73 * // Error Handling
74 * async.each(withMissingFileList, deleteFile, function(err){
75 * console.log(err);
76 * // [ Error: ENOENT: no such file or directory ]
77 * // since dir4/file2.txt does not exist
78 * // dir1/file1.txt could have been deleted
79 * });
80 *
81 * // Using Promises
82 * async.each(fileList, deleteFile)
83 * .then( () => {
84 * console.log('All files have been deleted successfully');
85 * }).catch( err => {
86 * console.log(err);
87 * });
88 *
89 * // Error Handling
90 * async.each(fileList, deleteFile)
91 * .then( () => {
92 * console.log('All files have been deleted successfully');
93 * }).catch( err => {
94 * console.log(err);
95 * // [ Error: ENOENT: no such file or directory ]
96 * // since dir4/file2.txt does not exist
97 * // dir1/file1.txt could have been deleted
98 * });
99 *
100 * // Using async/await
101 * async () => {
102 * try {
103 * await async.each(files, deleteFile);
104 * }
105 * catch (err) {
106 * console.log(err);
107 * }
108 * }
109 *
110 * // Error Handling
111 * async () => {
112 * try {
113 * await async.each(withMissingFileList, deleteFile);
114 * }
115 * catch (err) {
116 * console.log(err);
117 * // [ Error: ENOENT: no such file or directory ]
118 * // since dir4/file2.txt does not exist
119 * // dir1/file1.txt could have been deleted
120 * }
121 * }
122 *
123 */
124function eachLimit(coll, iteratee, callback) {
125 return (0, _eachOf2.default)(coll, (0, _withoutIndex2.default)((0, _wrapAsync2.default)(iteratee)), callback);
126}
127
128exports.default = (0, _awaitify2.default)(eachLimit, 3);
129module.exports = exports.default;
\No newline at end of file