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