each.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Applies the function `iteratee` to each item in `coll`, in parallel.
  13. * The `iteratee` is called with an item from the list, and a callback for when
  14. * it has finished. If the `iteratee` passes an error to its `callback`, the
  15. * main `callback` (for the `each` function) is immediately called with the
  16. * error.
  17. *
  18. * Note, that since this function applies `iteratee` to each item in parallel,
  19. * there is no guarantee that the iteratee functions will complete in order.
  20. *
  21. * @name each
  22. * @static
  23. * @memberOf module:Collections
  24. * @method
  25. * @alias forEach
  26. * @category Collection
  27. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  28. * @param {Function} iteratee - A function to apply to each item
  29. * in `coll`. The iteratee is passed a `callback(err)` which must be called once
  30. * it has completed. If no error has occurred, the `callback` should be run
  31. * without arguments or with an explicit `null` argument. The array index is not
  32. * passed to the iteratee. Invoked with (item, callback). If you need the index,
  33. * 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)(iteratee), callback);
  72. }
  73. module.exports = exports['default'];