compose.js 1.5 KB

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