forEach.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = eachLimit;
  6. var _eachOf = require('./eachOf');
  7. var _eachOf2 = _interopRequireDefault(_eachOf);
  8. var _withoutIndex = require('./internal/withoutIndex');
  9. var _withoutIndex2 = _interopRequireDefault(_withoutIndex);
  10. var _wrapAsync = require('./internal/wrapAsync');
  11. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Applies the function `iteratee` to each item in `coll`, in parallel.
  15. * The `iteratee` is called with an item from the list, and a callback for when
  16. * it has finished. If the `iteratee` passes an error to its `callback`, the
  17. * main `callback` (for the `each` function) is immediately called with the
  18. * error.
  19. *
  20. * Note, that since this function applies `iteratee` to each item in parallel,
  21. * there is no guarantee that the iteratee functions will complete in order.
  22. *
  23. * @name each
  24. * @static
  25. * @memberOf module:Collections
  26. * @method
  27. * @alias forEach
  28. * @category Collection
  29. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  30. * @param {AsyncFunction} iteratee - An async function to apply to
  31. * each item in `coll`. Invoked with (item, callback).
  32. * The array index is not passed to the iteratee.
  33. * If you need the index, use `eachOf`.
  34. * @param {Function} [callback] - A callback which is called when all
  35. * `iteratee` functions have finished, or an error occurs. Invoked with (err).
  36. * @example
  37. *
  38. * // assuming openFiles is an array of file names and saveFile is a function
  39. * // to save the modified contents of that file:
  40. *
  41. * async.each(openFiles, saveFile, function(err){
  42. * // if any of the saves produced an error, err would equal that error
  43. * });
  44. *
  45. * // assuming openFiles is an array of file names
  46. * async.each(openFiles, function(file, callback) {
  47. *
  48. * // Perform operation on file here.
  49. * console.log('Processing file ' + file);
  50. *
  51. * if( file.length > 32 ) {
  52. * console.log('This file name is too long');
  53. * callback('File name too long');
  54. * } else {
  55. * // Do work to process file here
  56. * console.log('File processed');
  57. * callback();
  58. * }
  59. * }, function(err) {
  60. * // if any of the file processing produced an error, err would equal that error
  61. * if( err ) {
  62. * // One of the iterations produced an error.
  63. * // All processing will now stop.
  64. * console.log('A file failed to process');
  65. * } else {
  66. * console.log('All files have been processed successfully');
  67. * }
  68. * });
  69. */
  70. function eachLimit(coll, iteratee, callback) {
  71. (0, _eachOf2.default)(coll, (0, _withoutIndex2.default)((0, _wrapAsync2.default)(iteratee)), callback);
  72. }
  73. module.exports = exports['default'];