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

Node.js中异步任务常导致回调地狱,Promise提供了解决方案。使用.all()可等待所有任务完成,.then()确保任务顺序执行。通过将任务转化为Promise并串联,可以更优雅地管理异步流程。本文将探讨如何结合.all()和.then()实现高效异步控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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数组。是一组“任务”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值