1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = seq;
|
7 |
|
8 | var _reduce = require('./reduce');
|
9 |
|
10 | var _reduce2 = _interopRequireDefault(_reduce);
|
11 |
|
12 | var _wrapAsync = require('./internal/wrapAsync');
|
13 |
|
14 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
15 |
|
16 | var _promiseCallback = require('./internal/promiseCallback');
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | /**
|
21 | * Version of the compose function that is more natural to read. Each function
|
22 | * consumes the return value of the previous function. It is the equivalent of
|
23 | * [compose]{@link module:ControlFlow.compose} with the arguments reversed.
|
24 | *
|
25 | * Each function is executed with the `this` binding of the composed function.
|
26 | *
|
27 | * @name seq
|
28 | * @static
|
29 | * @memberOf module:ControlFlow
|
30 | * @method
|
31 | * @see [async.compose]{@link module:ControlFlow.compose}
|
32 | * @category Control Flow
|
33 | * @param {...AsyncFunction} functions - the asynchronous functions to compose
|
34 | * @returns {Function} a function that composes the `functions` in order
|
35 | * @example
|
36 | *
|
37 | * // Requires lodash (or underscore), express3 and dresende's orm2.
|
38 | * // Part of an app, that fetches cats of the logged user.
|
39 | * // This example uses `seq` function to avoid overnesting and error
|
40 | * // handling clutter.
|
41 | * app.get('/cats', function(request, response) {
|
42 | * var User = request.models.User;
|
43 | * async.seq(
|
44 | * _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
|
45 | * function(user, fn) {
|
46 | * user.getCats(fn); // 'getCats' has signature (callback(err, data))
|
47 | * }
|
48 | * )(req.session.user_id, function (err, cats) {
|
49 | * if (err) {
|
50 | * console.error(err);
|
51 | * response.json({ status: 'error', message: err.message });
|
52 | * } else {
|
53 | * response.json({ status: 'ok', message: 'Cats found', data: cats });
|
54 | * }
|
55 | * });
|
56 | * });
|
57 | */
|
58 | function seq(...functions) {
|
59 | var _functions = functions.map(_wrapAsync2.default);
|
60 | return function (...args) {
|
61 | var that = this;
|
62 |
|
63 | var cb = args[args.length - 1];
|
64 | if (typeof cb == 'function') {
|
65 | args.pop();
|
66 | } else {
|
67 | cb = (0, _promiseCallback.promiseCallback)();
|
68 | }
|
69 |
|
70 | (0, _reduce2.default)(_functions, args, (newargs, fn, iterCb) => {
|
71 | fn.apply(that, newargs.concat((err, ...nextargs) => {
|
72 | iterCb(err, nextargs);
|
73 | }));
|
74 | }, (err, results) => cb(err, ...results));
|
75 |
|
76 | return cb[_promiseCallback.PROMISE_SYMBOL];
|
77 | };
|
78 | }
|
79 | module.exports = exports['default']; |
\ | No newline at end of file |