times.js

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