uni-ticket-system/node_modules/licia/waterfall.js
2023-12-05 10:11:10 +08:00

28 lines
666 B
JavaScript

var noop = require('./noop');
var nextTick = require('./nextTick');
var restArgs = require('./restArgs');
exports = function(tasks, cb) {
cb = cb || noop;
var current = 0;
var taskCb = restArgs(function(err, args) {
if (++current >= tasks.length || err) {
args.unshift(err);
nextTick(function() {
cb.apply(null, args);
});
} else {
args.push(taskCb);
tasks[current].apply(null, args);
}
});
if (tasks.length) {
tasks[0](taskCb);
} else {
nextTick(function() {
cb();
});
}
};
module.exports = exports;