프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / async / seq.js

이력 | 보기 | 이력해설 | 다운로드 (2.55 KB)

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