Q.js手册

序列

If you have a number of promise-producing functions that need to be run sequentially, you can of course do so manually:

return foo(initialVal).then(bar).then(baz).then(qux);

However, if you want to run a dynamically constructed sequence of functions, you'll want something like this:

var funcs = [foo, bar, baz, qux];

var result = Q(initialVal);
funcs.forEach(function (f) {
    result = result.then(f);
});
return result;

你可以使用 reduce 让这个稍微更紧凑些:

return funcs.reduce(function (soFar, f) {
    return soFar.then(f);
}, Q(initialVal));

或者,你可以使用超小型版本:

return funcs.reduce(Q.when, Q(initialVal));