times.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = times;
  6. var _timesLimit = require('./timesLimit.js');
  7. var _timesLimit2 = _interopRequireDefault(_timesLimit);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * Calls the `iteratee` function `n` times, and accumulates results in the same
  11. * manner you would use with [map]{@link module:Collections.map}.
  12. *
  13. * @name times
  14. * @static
  15. * @memberOf module:ControlFlow
  16. * @method
  17. * @see [async.map]{@link module:Collections.map}
  18. * @category Control Flow
  19. * @param {number} n - The number of times to run the function.
  20. * @param {AsyncFunction} iteratee - The async function to call `n` times.
  21. * Invoked with the iteration index and a callback: (n, next).
  22. * @param {Function} callback - see {@link module:Collections.map}.
  23. * @returns {Promise} a promise, if no callback is provided
  24. * @example
  25. *
  26. * // Pretend this is some complicated async factory
  27. * var createUser = function(id, callback) {
  28. * callback(null, {
  29. * id: 'user' + id
  30. * });
  31. * };
  32. *
  33. * // generate 5 users
  34. * async.times(5, function(n, next) {
  35. * createUser(n, function(err, user) {
  36. * next(err, user);
  37. * });
  38. * }, function(err, users) {
  39. * // we should now have 5 users
  40. * });
  41. */
  42. function times(n, iteratee, callback) {
  43. return (0, _timesLimit2.default)(n, Infinity, iteratee, callback);
  44. }
  45. module.exports = exports['default'];