您的位置:首页 > Web前端 > Node.js

用Promise让Node的异步任务顺序执行

2016-07-31 13:56 316 查看
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
数组。是一组“任务”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: