function runner (fns, context, next) {
var last = fns.length - 1;
(function run(pos) {
fns[pos].call(context, function (err) {
if (err || pos === last) return next(err);
run(++pos);
});
})(0);
}
var fns = [
test1,
test2,
test3,
test4,
test5
];
function next(){
console.log("next");
}
function test1 (done) {
console.log("test1");
done(null);
}
function test2 (done) {
console.log("test2");
done(null);
}
function test3 (done) {
console.log("test3");
done(null);
}
function test4 (done) {
console.log("test4");
var err = "test4";
done(err);
}
function test5 (done) {
console.log("test5");
done(null);
}
runner(fns, this, next);
结果:
test1
test2
test3
test4
next