Node的机制导很多的任务执行是异步的,一般用回调处理任务的结果。多任务就会导致多层嵌套。
于是Promise就被用来处理这个事情。尤其是bluebird的Promise实现功能丰富。
如果需要一大串的任务全部执行完成之后继续后面的,那么就用Promise.all方法,如果要任务顺序执行,并把每次的结果单独处理就用Promise.reduce方法。
这两个方法组合起来就可以发挥更加大的威力:
/ * # Pipeline Utility * * Based on pipeline.js from when.js: * https://github.com/cujojs/when/blob/3.7.4/pipeline.js */var Promise = require('bluebird');function pipeline(tasks /* initial arguments */) { var args = Array.prototype.slice.call(arguments, 1), runTask = function (task, args) { // Self-optimizing function to run first task with multiple // args using apply, but subsequent tasks via direct invocation runTask = function (task, arg) { return task(arg); }; return task.apply(null, args); }; // Resolve any promises for the arguments passed in first return Promise.all(args).then(function (args) { // Iterate through the tasks passing args from one into the next return Promise.reduce(tasks, function (arg, task) { return runTask(task, arg); }, args); });}module.exports = pipeline;首先使用Promise.all把参数都转化成一个个的Promise然后,用Promise.reduce挨个执行任务,并且把每一个任务的结果依次传递到下一个任务。
来看看怎么用的。
function modelQuery(options) {
return dataProvider.Post.findPage(options);
}
// Push all of our tasks into a `tasks` array in the correct order tasks = [ utils.validate(docName, {opts: permittedOptions}), utils.handlePublicPermissions(docName, 'browse'), utils.convertOptions(allowedIncludes), modelQuery ]; // Pipeline calls each task passing the result of one to be the arguments for the next return pipeline(tasks, options);注意: tasks数组的每一个成员都返回一个方法。所以tasks是一个Function数组。是一组“任务”。
关键字:node.js, promise, tasks, args