inject.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _eachOfSeries = require('./eachOfSeries.js');
  6. var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
  7. var _once = require('./internal/once.js');
  8. var _once2 = _interopRequireDefault(_once);
  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. * Reduces `coll` into a single value using an async `iteratee` to return each
  16. * successive step. `memo` is the initial state of the reduction. This function
  17. * only operates in series.
  18. *
  19. * For performance reasons, it may make sense to split a call to this function
  20. * into a parallel map, and then use the normal `Array.prototype.reduce` on the
  21. * results. This function is for situations where each step in the reduction
  22. * needs to be async; if you can get the data before reducing it, then it's
  23. * probably a good idea to do so.
  24. *
  25. * @name reduce
  26. * @static
  27. * @memberOf module:Collections
  28. * @method
  29. * @alias inject
  30. * @alias foldl
  31. * @category Collection
  32. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  33. * @param {*} memo - The initial state of the reduction.
  34. * @param {AsyncFunction} iteratee - A function applied to each item in the
  35. * array to produce the next step in the reduction.
  36. * The `iteratee` should complete with the next state of the reduction.
  37. * If the iteratee completes with an error, the reduction is stopped and the
  38. * main `callback` is immediately called with the error.
  39. * Invoked with (memo, item, callback).
  40. * @param {Function} [callback] - A callback which is called after all the
  41. * `iteratee` functions have finished. Result is the reduced value. Invoked with
  42. * (err, result).
  43. * @returns {Promise} a promise, if no callback is passed
  44. * @example
  45. *
  46. * // file1.txt is a file that is 1000 bytes in size
  47. * // file2.txt is a file that is 2000 bytes in size
  48. * // file3.txt is a file that is 3000 bytes in size
  49. * // file4.txt does not exist
  50. *
  51. * const fileList = ['file1.txt','file2.txt','file3.txt'];
  52. * const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt'];
  53. *
  54. * // asynchronous function that computes the file size in bytes
  55. * // file size is added to the memoized value, then returned
  56. * function getFileSizeInBytes(memo, file, callback) {
  57. * fs.stat(file, function(err, stat) {
  58. * if (err) {
  59. * return callback(err);
  60. * }
  61. * callback(null, memo + stat.size);
  62. * });
  63. * }
  64. *
  65. * // Using callbacks
  66. * async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) {
  67. * if (err) {
  68. * console.log(err);
  69. * } else {
  70. * console.log(result);
  71. * // 6000
  72. * // which is the sum of the file sizes of the three files
  73. * }
  74. * });
  75. *
  76. * // Error Handling
  77. * async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) {
  78. * if (err) {
  79. * console.log(err);
  80. * // [ Error: ENOENT: no such file or directory ]
  81. * } else {
  82. * console.log(result);
  83. * }
  84. * });
  85. *
  86. * // Using Promises
  87. * async.reduce(fileList, 0, getFileSizeInBytes)
  88. * .then( result => {
  89. * console.log(result);
  90. * // 6000
  91. * // which is the sum of the file sizes of the three files
  92. * }).catch( err => {
  93. * console.log(err);
  94. * });
  95. *
  96. * // Error Handling
  97. * async.reduce(withMissingFileList, 0, getFileSizeInBytes)
  98. * .then( result => {
  99. * console.log(result);
  100. * }).catch( err => {
  101. * console.log(err);
  102. * // [ Error: ENOENT: no such file or directory ]
  103. * });
  104. *
  105. * // Using async/await
  106. * async () => {
  107. * try {
  108. * let result = await async.reduce(fileList, 0, getFileSizeInBytes);
  109. * console.log(result);
  110. * // 6000
  111. * // which is the sum of the file sizes of the three files
  112. * }
  113. * catch (err) {
  114. * console.log(err);
  115. * }
  116. * }
  117. *
  118. * // Error Handling
  119. * async () => {
  120. * try {
  121. * let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes);
  122. * console.log(result);
  123. * }
  124. * catch (err) {
  125. * console.log(err);
  126. * // [ Error: ENOENT: no such file or directory ]
  127. * }
  128. * }
  129. *
  130. */
  131. function reduce(coll, memo, iteratee, callback) {
  132. callback = (0, _once2.default)(callback);
  133. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  134. return (0, _eachOfSeries2.default)(coll, (x, i, iterCb) => {
  135. _iteratee(memo, x, (err, v) => {
  136. memo = v;
  137. iterCb(err);
  138. });
  139. }, err => callback(err, memo));
  140. }
  141. exports.default = (0, _awaitify2.default)(reduce, 4);
  142. module.exports = exports['default'];