Projekt

Obecné

Profil

Stáhnout (2.92 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
Object.defineProperty(exports, "__esModule", {
4
    value: true
5
});
6
exports.default = seq;
7

    
8
var _noop = require('lodash/noop');
9

    
10
var _noop2 = _interopRequireDefault(_noop);
11

    
12
var _slice = require('./internal/slice');
13

    
14
var _slice2 = _interopRequireDefault(_slice);
15

    
16
var _reduce = require('./reduce');
17

    
18
var _reduce2 = _interopRequireDefault(_reduce);
19

    
20
var _wrapAsync = require('./internal/wrapAsync');
21

    
22
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
23

    
24
var _arrayMap = require('lodash/_arrayMap');
25

    
26
var _arrayMap2 = _interopRequireDefault(_arrayMap);
27

    
28
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29

    
30
/**
31
 * Version of the compose function that is more natural to read. Each function
32
 * consumes the return value of the previous function. It is the equivalent of
33
 * [compose]{@link module:ControlFlow.compose} with the arguments reversed.
34
 *
35
 * Each function is executed with the `this` binding of the composed function.
36
 *
37
 * @name seq
38
 * @static
39
 * @memberOf module:ControlFlow
40
 * @method
41
 * @see [async.compose]{@link module:ControlFlow.compose}
42
 * @category Control Flow
43
 * @param {...AsyncFunction} functions - the asynchronous functions to compose
44
 * @returns {Function} a function that composes the `functions` in order
45
 * @example
46
 *
47
 * // Requires lodash (or underscore), express3 and dresende's orm2.
48
 * // Part of an app, that fetches cats of the logged user.
49
 * // This example uses `seq` function to avoid overnesting and error
50
 * // handling clutter.
51
 * app.get('/cats', function(request, response) {
52
 *     var User = request.models.User;
53
 *     async.seq(
54
 *         _.bind(User.get, User),  // 'User.get' has signature (id, callback(err, data))
55
 *         function(user, fn) {
56
 *             user.getCats(fn);      // 'getCats' has signature (callback(err, data))
57
 *         }
58
 *     )(req.session.user_id, function (err, cats) {
59
 *         if (err) {
60
 *             console.error(err);
61
 *             response.json({ status: 'error', message: err.message });
62
 *         } else {
63
 *             response.json({ status: 'ok', message: 'Cats found', data: cats });
64
 *         }
65
 *     });
66
 * });
67
 */
68
function seq() /*...functions*/{
69
    var _functions = (0, _arrayMap2.default)(arguments, _wrapAsync2.default);
70
    return function () /*...args*/{
71
        var args = (0, _slice2.default)(arguments);
72
        var that = this;
73

    
74
        var cb = args[args.length - 1];
75
        if (typeof cb == 'function') {
76
            args.pop();
77
        } else {
78
            cb = _noop2.default;
79
        }
80

    
81
        (0, _reduce2.default)(_functions, args, function (newargs, fn, cb) {
82
            fn.apply(that, newargs.concat(function (err /*, ...nextargs*/) {
83
                var nextargs = (0, _slice2.default)(arguments, 1);
84
                cb(err, nextargs);
85
            }));
86
        }, function (err, results) {
87
            cb.apply(that, [err].concat(results));
88
        });
89
    };
90
}
91
module.exports = exports['default'];
(88-88/105)