each.js

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