UNPKG

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