mapValuesLimit.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _eachOfLimit = require('./internal/eachOfLimit.js');
  6. var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
  7. var _awaitify = require('./internal/awaitify.js');
  8. var _awaitify2 = _interopRequireDefault(_awaitify);
  9. var _once = require('./internal/once.js');
  10. var _once2 = _interopRequireDefault(_once);
  11. var _wrapAsync = require('./internal/wrapAsync.js');
  12. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * The same as [`mapValues`]{@link module:Collections.mapValues} but runs a maximum of `limit` async operations at a
  16. * time.
  17. *
  18. * @name mapValuesLimit
  19. * @static
  20. * @memberOf module:Collections
  21. * @method
  22. * @see [async.mapValues]{@link module:Collections.mapValues}
  23. * @category Collection
  24. * @param {Object} obj - A collection to iterate over.
  25. * @param {number} limit - The maximum number of async operations at a time.
  26. * @param {AsyncFunction} iteratee - A function to apply to each value and key
  27. * in `coll`.
  28. * The iteratee should complete with the transformed value as its result.
  29. * Invoked with (value, key, callback).
  30. * @param {Function} [callback] - A callback which is called when all `iteratee`
  31. * functions have finished, or an error occurs. `result` is a new object consisting
  32. * of each key from `obj`, with each transformed value on the right-hand side.
  33. * Invoked with (err, result).
  34. * @returns {Promise} a promise, if no callback is passed
  35. */
  36. function mapValuesLimit(obj, limit, iteratee, callback) {
  37. callback = (0, _once2.default)(callback);
  38. var newObj = {};
  39. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  40. return (0, _eachOfLimit2.default)(limit)(obj, (val, key, next) => {
  41. _iteratee(val, key, (err, result) => {
  42. if (err) return next(err);
  43. newObj[key] = result;
  44. next(err);
  45. });
  46. }, err => callback(err, newObj));
  47. }
  48. exports.default = (0, _awaitify2.default)(mapValuesLimit, 4);
  49. module.exports = exports['default'];