compose.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _seq = require('./seq');
  6. var _seq2 = _interopRequireDefault(_seq);
  7. var _rest = require('./internal/rest');
  8. var _rest2 = _interopRequireDefault(_rest);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Creates a function which is a composition of the passed asynchronous
  12. * functions. Each function consumes the return value of the function that
  13. * follows. Composing functions `f()`, `g()`, and `h()` would produce the result
  14. * of `f(g(h()))`, only this version uses callbacks to obtain the return values.
  15. *
  16. * Each function is executed with the `this` binding of the composed function.
  17. *
  18. * @name compose
  19. * @static
  20. * @memberOf module:ControlFlow
  21. * @method
  22. * @category Control Flow
  23. * @param {...Function} functions - the asynchronous functions to compose
  24. * @returns {Function} an asynchronous function that is the composed
  25. * asynchronous `functions`
  26. * @example
  27. *
  28. * function add1(n, callback) {
  29. * setTimeout(function () {
  30. * callback(null, n + 1);
  31. * }, 10);
  32. * }
  33. *
  34. * function mul3(n, callback) {
  35. * setTimeout(function () {
  36. * callback(null, n * 3);
  37. * }, 10);
  38. * }
  39. *
  40. * var add1mul3 = async.compose(mul3, add1);
  41. * add1mul3(4, function (err, result) {
  42. * // result now equals 15
  43. * });
  44. */
  45. exports.default = (0, _rest2.default)(function (args) {
  46. return _seq2.default.apply(null, args.reverse());
  47. });
  48. module.exports = exports['default'];