apply.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _rest = require('./internal/rest');
  6. var _rest2 = _interopRequireDefault(_rest);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. * Creates a continuation function with some arguments already applied.
  10. *
  11. * Useful as a shorthand when combined with other control flow functions. Any
  12. * arguments passed to the returned function are added to the arguments
  13. * originally passed to apply.
  14. *
  15. * @name apply
  16. * @static
  17. * @memberOf module:Utils
  18. * @method
  19. * @category Util
  20. * @param {Function} function - The function you want to eventually apply all
  21. * arguments to. Invokes with (arguments...).
  22. * @param {...*} arguments... - Any number of arguments to automatically apply
  23. * when the continuation is called.
  24. * @example
  25. *
  26. * // using apply
  27. * async.parallel([
  28. * async.apply(fs.writeFile, 'testfile1', 'test1'),
  29. * async.apply(fs.writeFile, 'testfile2', 'test2')
  30. * ]);
  31. *
  32. *
  33. * // the same process without using apply
  34. * async.parallel([
  35. * function(callback) {
  36. * fs.writeFile('testfile1', 'test1', callback);
  37. * },
  38. * function(callback) {
  39. * fs.writeFile('testfile2', 'test2', callback);
  40. * }
  41. * ]);
  42. *
  43. * // It's possible to pass any number of additional arguments when calling the
  44. * // continuation:
  45. *
  46. * node> var fn = async.apply(sys.puts, 'one');
  47. * node> fn('two', 'three');
  48. * one
  49. * two
  50. * three
  51. */
  52. exports.default = (0, _rest2.default)(function (fn, args) {
  53. return (0, _rest2.default)(function (callArgs) {
  54. return fn.apply(null, args.concat(callArgs));
  55. });
  56. });
  57. module.exports = exports['default'];