forEach.js
1 'use strict'; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 7 var _eachOf = require('./eachOf'); 8 9 var _eachOf2 = _interopRequireDefault(_eachOf); 10 11 var _withoutIndex = require('./internal/withoutIndex'); 12 13 var _withoutIndex2 = _interopRequireDefault(_withoutIndex); 14 15 var _wrapAsync = require('./internal/wrapAsync'); 16 17 var _wrapAsync2 = _interopRequireDefault(_wrapAsync); 18 19 var _awaitify = require('./internal/awaitify'); 20 21 var _awaitify2 = _interopRequireDefault(_awaitify); 22 23 function _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 * // assuming openFiles is an array of file names and saveFile is a function 52 * // to save the modified contents of that file: 53 * 54 * async.each(openFiles, saveFile, function(err){ 55 * // if any of the saves produced an error, err would equal that error 56 * }); 57 * 58 * // assuming openFiles is an array of file names 59 * async.each(openFiles, function(file, callback) { 60 * 61 * // Perform operation on file here. 62 * console.log('Processing file ' + file); 63 * 64 * if( file.length > 32 ) { 65 * console.log('This file name is too long'); 66 * callback('File name too long'); 67 * } else { 68 * // Do work to process file here 69 * console.log('File processed'); 70 * callback(); 71 * } 72 * }, function(err) { 73 * // if any of the file processing produced an error, err would equal that error 74 * if( err ) { 75 * // One of the iterations produced an error. 76 * // All processing will now stop. 77 * console.log('A file failed to process'); 78 * } else { 79 * console.log('All files have been processed successfully'); 80 * } 81 * }); 82 */ 83 function eachLimit(coll, iteratee, callback) { 84 return (0, _eachOf2.default)(coll, (0, _withoutIndex2.default)((0, _wrapAsync2.default)(iteratee)), callback); 85 } 86 87 exports.default = (0, _awaitify2.default)(eachLimit, 3); 88 module.exports = exports['default'];