eachOf.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _isArrayLike = require('./internal/isArrayLike.js');
  6. var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
  7. var _breakLoop = require('./internal/breakLoop.js');
  8. var _breakLoop2 = _interopRequireDefault(_breakLoop);
  9. var _eachOfLimit = require('./eachOfLimit.js');
  10. var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
  11. var _once = require('./internal/once.js');
  12. var _once2 = _interopRequireDefault(_once);
  13. var _onlyOnce = require('./internal/onlyOnce.js');
  14. var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
  15. var _wrapAsync = require('./internal/wrapAsync.js');
  16. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  17. var _awaitify = require('./internal/awaitify.js');
  18. var _awaitify2 = _interopRequireDefault(_awaitify);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. // eachOf implementation optimized for array-likes
  21. function eachOfArrayLike(coll, iteratee, callback) {
  22. callback = (0, _once2.default)(callback);
  23. var index = 0,
  24. completed = 0,
  25. { length } = coll,
  26. canceled = false;
  27. if (length === 0) {
  28. callback(null);
  29. }
  30. function iteratorCallback(err, value) {
  31. if (err === false) {
  32. canceled = true;
  33. }
  34. if (canceled === true) return;
  35. if (err) {
  36. callback(err);
  37. } else if (++completed === length || value === _breakLoop2.default) {
  38. callback(null);
  39. }
  40. }
  41. for (; index < length; index++) {
  42. iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback));
  43. }
  44. }
  45. // a generic version of eachOf which can handle array, object, and iterator cases.
  46. function eachOfGeneric(coll, iteratee, callback) {
  47. return (0, _eachOfLimit2.default)(coll, Infinity, iteratee, callback);
  48. }
  49. /**
  50. * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
  51. * to the iteratee.
  52. *
  53. * @name eachOf
  54. * @static
  55. * @memberOf module:Collections
  56. * @method
  57. * @alias forEachOf
  58. * @category Collection
  59. * @see [async.each]{@link module:Collections.each}
  60. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  61. * @param {AsyncFunction} iteratee - A function to apply to each
  62. * item in `coll`.
  63. * The `key` is the item's key, or index in the case of an array.
  64. * Invoked with (item, key, callback).
  65. * @param {Function} [callback] - A callback which is called when all
  66. * `iteratee` functions have finished, or an error occurs. Invoked with (err).
  67. * @returns {Promise} a promise, if a callback is omitted
  68. * @example
  69. *
  70. * // dev.json is a file containing a valid json object config for dev environment
  71. * // dev.json is a file containing a valid json object config for test environment
  72. * // prod.json is a file containing a valid json object config for prod environment
  73. * // invalid.json is a file with a malformed json object
  74. *
  75. * let configs = {}; //global variable
  76. * let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'};
  77. * let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'};
  78. *
  79. * // asynchronous function that reads a json file and parses the contents as json object
  80. * function parseFile(file, key, callback) {
  81. * fs.readFile(file, "utf8", function(err, data) {
  82. * if (err) return calback(err);
  83. * try {
  84. * configs[key] = JSON.parse(data);
  85. * } catch (e) {
  86. * return callback(e);
  87. * }
  88. * callback();
  89. * });
  90. * }
  91. *
  92. * // Using callbacks
  93. * async.forEachOf(validConfigFileMap, parseFile, function (err) {
  94. * if (err) {
  95. * console.error(err);
  96. * } else {
  97. * console.log(configs);
  98. * // configs is now a map of JSON data, e.g.
  99. * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
  100. * }
  101. * });
  102. *
  103. * //Error handing
  104. * async.forEachOf(invalidConfigFileMap, parseFile, function (err) {
  105. * if (err) {
  106. * console.error(err);
  107. * // JSON parse error exception
  108. * } else {
  109. * console.log(configs);
  110. * }
  111. * });
  112. *
  113. * // Using Promises
  114. * async.forEachOf(validConfigFileMap, parseFile)
  115. * .then( () => {
  116. * console.log(configs);
  117. * // configs is now a map of JSON data, e.g.
  118. * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
  119. * }).catch( err => {
  120. * console.error(err);
  121. * });
  122. *
  123. * //Error handing
  124. * async.forEachOf(invalidConfigFileMap, parseFile)
  125. * .then( () => {
  126. * console.log(configs);
  127. * }).catch( err => {
  128. * console.error(err);
  129. * // JSON parse error exception
  130. * });
  131. *
  132. * // Using async/await
  133. * async () => {
  134. * try {
  135. * let result = await async.forEachOf(validConfigFileMap, parseFile);
  136. * console.log(configs);
  137. * // configs is now a map of JSON data, e.g.
  138. * // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
  139. * }
  140. * catch (err) {
  141. * console.log(err);
  142. * }
  143. * }
  144. *
  145. * //Error handing
  146. * async () => {
  147. * try {
  148. * let result = await async.forEachOf(invalidConfigFileMap, parseFile);
  149. * console.log(configs);
  150. * }
  151. * catch (err) {
  152. * console.log(err);
  153. * // JSON parse error exception
  154. * }
  155. * }
  156. *
  157. */
  158. function eachOf(coll, iteratee, callback) {
  159. var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric;
  160. return eachOfImplementation(coll, (0, _wrapAsync2.default)(iteratee), callback);
  161. }
  162. exports.default = (0, _awaitify2.default)(eachOf, 3);
  163. module.exports = exports.default;