프로젝트

일반

사용자정보

통계
| 개정판:

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

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

1
'use strict';
2

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

    
7
exports.default = function (tasks, callback) {
8
    callback = (0, _once2.default)(callback || _noop2.default);
9
    if (!(0, _isArray2.default)(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
10
    if (!tasks.length) return callback();
11
    var taskIndex = 0;
12

    
13
    function nextTask(args) {
14
        if (taskIndex === tasks.length) {
15
            return callback.apply(null, [null].concat(args));
16
        }
17

    
18
        var taskCallback = (0, _onlyOnce2.default)((0, _rest2.default)(function (err, args) {
19
            if (err) {
20
                return callback.apply(null, [err].concat(args));
21
            }
22
            nextTask(args);
23
        }));
24

    
25
        args.push(taskCallback);
26

    
27
        var task = tasks[taskIndex++];
28
        task.apply(null, args);
29
    }
30

    
31
    nextTask([]);
32
};
33

    
34
var _isArray = require('lodash/isArray');
35

    
36
var _isArray2 = _interopRequireDefault(_isArray);
37

    
38
var _noop = require('lodash/noop');
39

    
40
var _noop2 = _interopRequireDefault(_noop);
41

    
42
var _once = require('./internal/once');
43

    
44
var _once2 = _interopRequireDefault(_once);
45

    
46
var _rest = require('./internal/rest');
47

    
48
var _rest2 = _interopRequireDefault(_rest);
49

    
50
var _onlyOnce = require('./internal/onlyOnce');
51

    
52
var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
53

    
54
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55

    
56
module.exports = exports['default'];
57

    
58
/**
59
 * Runs the `tasks` array of functions in series, each passing their results to
60
 * the next in the array. However, if any of the `tasks` pass an error to their
61
 * own callback, the next function is not executed, and the main `callback` is
62
 * immediately called with the error.
63
 *
64
 * @name waterfall
65
 * @static
66
 * @memberOf module:ControlFlow
67
 * @method
68
 * @category Control Flow
69
 * @param {Array} tasks - An array of functions to run, each function is passed
70
 * a `callback(err, result1, result2, ...)` it must call on completion. The
71
 * first argument is an error (which can be `null`) and any further arguments
72
 * will be passed as arguments in order to the next task.
73
 * @param {Function} [callback] - An optional callback to run once all the
74
 * functions have completed. This will be passed the results of the last task's
75
 * callback. Invoked with (err, [results]).
76
 * @returns undefined
77
 * @example
78
 *
79
 * async.waterfall([
80
 *     function(callback) {
81
 *         callback(null, 'one', 'two');
82
 *     },
83
 *     function(arg1, arg2, callback) {
84
 *         // arg1 now equals 'one' and arg2 now equals 'two'
85
 *         callback(null, 'three');
86
 *     },
87
 *     function(arg1, callback) {
88
 *         // arg1 now equals 'three'
89
 *         callback(null, 'done');
90
 *     }
91
 * ], function (err, result) {
92
 *     // result now equals 'done'
93
 * });
94
 *
95
 * // Or, with named functions:
96
 * async.waterfall([
97
 *     myFirstFunction,
98
 *     mySecondFunction,
99
 *     myLastFunction,
100
 * ], function (err, result) {
101
 *     // result now equals 'done'
102
 * });
103
 * function myFirstFunction(callback) {
104
 *     callback(null, 'one', 'two');
105
 * }
106
 * function mySecondFunction(arg1, arg2, callback) {
107
 *     // arg1 now equals 'one' and arg2 now equals 'two'
108
 *     callback(null, 'three');
109
 * }
110
 * function myLastFunction(arg1, callback) {
111
 *     // arg1 now equals 'three'
112
 *     callback(null, 'done');
113
 * }
114
 */