each.js 3.9 KB

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