用Promise让Node的异步任务顺序执行
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数组。是一组“任务”。
相关推荐
88254251 2020-11-01
MarukoMa 2020-09-02
88234852 2020-09-15
陈旭阳 2020-08-31
whynotgonow 2020-08-19
前端开发Kingcean 2020-07-30
whynotgonow 2020-07-29
bowean 2020-07-08
前端开发Kingcean 2020-07-08
88520191 2020-07-05
前端开发Kingcean 2020-06-27
88481456 2020-06-18
whynotgonow 2020-06-16
88520191 2020-06-13
88520191 2020-06-13
89500297 2020-06-13