map.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _map2 = require('./internal/map.js');
  6. var _map3 = _interopRequireDefault(_map2);
  7. var _eachOf = require('./eachOf.js');
  8. var _eachOf2 = _interopRequireDefault(_eachOf);
  9. var _awaitify = require('./internal/awaitify.js');
  10. var _awaitify2 = _interopRequireDefault(_awaitify);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Produces a new collection of values by mapping each value in `coll` through
  14. * the `iteratee` function. The `iteratee` is called with an item from `coll`
  15. * and a callback for when it has finished processing. Each of these callbacks
  16. * takes 2 arguments: an `error`, and the transformed item from `coll`. If
  17. * `iteratee` passes an error to its callback, the main `callback` (for the
  18. * `map` function) is immediately called with the error.
  19. *
  20. * Note, that since this function applies the `iteratee` to each item in
  21. * parallel, there is no guarantee that the `iteratee` functions will complete
  22. * in order. However, the results array will be in the same order as the
  23. * original `coll`.
  24. *
  25. * If `map` is passed an Object, the results will be an Array. The results
  26. * will roughly be in the order of the original Objects' keys (but this can
  27. * vary across JavaScript engines).
  28. *
  29. * @name map
  30. * @static
  31. * @memberOf module:Collections
  32. * @method
  33. * @category Collection
  34. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  35. * @param {AsyncFunction} iteratee - An async function to apply to each item in
  36. * `coll`.
  37. * The iteratee should complete with the transformed item.
  38. * Invoked with (item, callback).
  39. * @param {Function} [callback] - A callback which is called when all `iteratee`
  40. * functions have finished, or an error occurs. Results is an Array of the
  41. * transformed items from the `coll`. Invoked with (err, results).
  42. * @returns {Promise} a promise, if no callback is passed
  43. * @example
  44. *
  45. * // file1.txt is a file that is 1000 bytes in size
  46. * // file2.txt is a file that is 2000 bytes in size
  47. * // file3.txt is a file that is 3000 bytes in size
  48. * // file4.txt does not exist
  49. *
  50. * const fileList = ['file1.txt','file2.txt','file3.txt'];
  51. * const withMissingFileList = ['file1.txt','file2.txt','file4.txt'];
  52. *
  53. * // asynchronous function that returns the file size in bytes
  54. * function getFileSizeInBytes(file, callback) {
  55. * fs.stat(file, function(err, stat) {
  56. * if (err) {
  57. * return callback(err);
  58. * }
  59. * callback(null, stat.size);
  60. * });
  61. * }
  62. *
  63. * // Using callbacks
  64. * async.map(fileList, getFileSizeInBytes, function(err, results) {
  65. * if (err) {
  66. * console.log(err);
  67. * } else {
  68. * console.log(results);
  69. * // results is now an array of the file size in bytes for each file, e.g.
  70. * // [ 1000, 2000, 3000]
  71. * }
  72. * });
  73. *
  74. * // Error Handling
  75. * async.map(withMissingFileList, getFileSizeInBytes, function(err, results) {
  76. * if (err) {
  77. * console.log(err);
  78. * // [ Error: ENOENT: no such file or directory ]
  79. * } else {
  80. * console.log(results);
  81. * }
  82. * });
  83. *
  84. * // Using Promises
  85. * async.map(fileList, getFileSizeInBytes)
  86. * .then( results => {
  87. * console.log(results);
  88. * // results is now an array of the file size in bytes for each file, e.g.
  89. * // [ 1000, 2000, 3000]
  90. * }).catch( err => {
  91. * console.log(err);
  92. * });
  93. *
  94. * // Error Handling
  95. * async.map(withMissingFileList, getFileSizeInBytes)
  96. * .then( results => {
  97. * console.log(results);
  98. * }).catch( err => {
  99. * console.log(err);
  100. * // [ Error: ENOENT: no such file or directory ]
  101. * });
  102. *
  103. * // Using async/await
  104. * async () => {
  105. * try {
  106. * let results = await async.map(fileList, getFileSizeInBytes);
  107. * console.log(results);
  108. * // results is now an array of the file size in bytes for each file, e.g.
  109. * // [ 1000, 2000, 3000]
  110. * }
  111. * catch (err) {
  112. * console.log(err);
  113. * }
  114. * }
  115. *
  116. * // Error Handling
  117. * async () => {
  118. * try {
  119. * let results = await async.map(withMissingFileList, getFileSizeInBytes);
  120. * console.log(results);
  121. * }
  122. * catch (err) {
  123. * console.log(err);
  124. * // [ Error: ENOENT: no such file or directory ]
  125. * }
  126. * }
  127. *
  128. */
  129. function map(coll, iteratee, callback) {
  130. return (0, _map3.default)(_eachOf2.default, coll, iteratee, callback);
  131. }
  132. exports.default = (0, _awaitify2.default)(map, 3);
  133. module.exports = exports['default'];