flatMap.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _concatLimit = require('./concatLimit.js');
  6. var _concatLimit2 = _interopRequireDefault(_concatLimit);
  7. var _awaitify = require('./internal/awaitify.js');
  8. var _awaitify2 = _interopRequireDefault(_awaitify);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Applies `iteratee` to each item in `coll`, concatenating the results. Returns
  12. * the concatenated list. The `iteratee`s are called in parallel, and the
  13. * results are concatenated as they return. The results array will be returned in
  14. * the original order of `coll` passed to the `iteratee` function.
  15. *
  16. * @name concat
  17. * @static
  18. * @memberOf module:Collections
  19. * @method
  20. * @category Collection
  21. * @alias flatMap
  22. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  23. * @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
  24. * which should use an array as its result. Invoked with (item, callback).
  25. * @param {Function} [callback] - A callback which is called after all the
  26. * `iteratee` functions have finished, or an error occurs. Results is an array
  27. * containing the concatenated results of the `iteratee` function. Invoked with
  28. * (err, results).
  29. * @returns A Promise, if no callback is passed
  30. * @example
  31. *
  32. * // dir1 is a directory that contains file1.txt, file2.txt
  33. * // dir2 is a directory that contains file3.txt, file4.txt
  34. * // dir3 is a directory that contains file5.txt
  35. * // dir4 does not exist
  36. *
  37. * let directoryList = ['dir1','dir2','dir3'];
  38. * let withMissingDirectoryList = ['dir1','dir2','dir3', 'dir4'];
  39. *
  40. * // Using callbacks
  41. * async.concat(directoryList, fs.readdir, function(err, results) {
  42. * if (err) {
  43. * console.log(err);
  44. * } else {
  45. * console.log(results);
  46. * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
  47. * }
  48. * });
  49. *
  50. * // Error Handling
  51. * async.concat(withMissingDirectoryList, fs.readdir, function(err, results) {
  52. * if (err) {
  53. * console.log(err);
  54. * // [ Error: ENOENT: no such file or directory ]
  55. * // since dir4 does not exist
  56. * } else {
  57. * console.log(results);
  58. * }
  59. * });
  60. *
  61. * // Using Promises
  62. * async.concat(directoryList, fs.readdir)
  63. * .then(results => {
  64. * console.log(results);
  65. * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
  66. * }).catch(err => {
  67. * console.log(err);
  68. * });
  69. *
  70. * // Error Handling
  71. * async.concat(withMissingDirectoryList, fs.readdir)
  72. * .then(results => {
  73. * console.log(results);
  74. * }).catch(err => {
  75. * console.log(err);
  76. * // [ Error: ENOENT: no such file or directory ]
  77. * // since dir4 does not exist
  78. * });
  79. *
  80. * // Using async/await
  81. * async () => {
  82. * try {
  83. * let results = await async.concat(directoryList, fs.readdir);
  84. * console.log(results);
  85. * // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
  86. * } catch (err) {
  87. * console.log(err);
  88. * }
  89. * }
  90. *
  91. * // Error Handling
  92. * async () => {
  93. * try {
  94. * let results = await async.concat(withMissingDirectoryList, fs.readdir);
  95. * console.log(results);
  96. * } catch (err) {
  97. * console.log(err);
  98. * // [ Error: ENOENT: no such file or directory ]
  99. * // since dir4 does not exist
  100. * }
  101. * }
  102. *
  103. */
  104. function concat(coll, iteratee, callback) {
  105. return (0, _concatLimit2.default)(coll, Infinity, iteratee, callback);
  106. }
  107. exports.default = (0, _awaitify2.default)(concat, 3);
  108. module.exports = exports['default'];