如何使用 Q 正确中止 node.js 承诺链? [英] How to properly abort a node.js promise chain using Q?

查看:45
本文介绍了如何使用 Q 正确中止 node.js 承诺链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js 中使用 Q 模块,试图在以下情况下避免末日金字塔"我有很多步骤.例如:

I'm using the Q module for Node.js in attempts to avoid the "pyramid of doom" in scenarios where I have many steps. For example:

function doTask(task, callback)
{
    Q.ncall(task.step1, task)
    .then(function(result1){
        return Q.ncall(task.step2, task);
    })
    .then(function(result2){
        return Q.ncall(task.step3, task);
    })
    .fail(callback).end();
}

基本上这似乎有效;如果任何任务步骤抛出错误,则将其传递给回调(尽管我欢迎改进,因为我是 node.js 承诺的新手).但是,当我需要提前中止任务链时,我遇到了问题.例如,如果 result1 成功返回,我可能想提前调用回调并中止其余的,但我的尝试失败了...

Essentially this seems to work; if an error is thrown by any of the task steps, it is passed to the callback (though I would be welcome to improvements, as I am new to node.js promises). However, I have a problem when I need to abort the task-chain early. For example, if result1 is successfully returned I might want to call the callback early and abort the rest, but my attempts to do so are failing...

function doTask(task, callback)
{
    Q.ncall(task.step1, task)
    .then(function(result1){
        if(result1)
        {// the rest of the task chain is unnecessary 
            console.log('aborting!');
            callback(null, result1);
            return null;
        }
        return Q.ncall(task.step2, task);
    })
    .then(function(result2){
        console.log('doing step 3...');
        return Q.ncall(task.step3, task);
    })
    .fail(callback).end();
}

在这个例子中,我看到了正在中止!"并打印执行第 3 步...".

In this example, I see both "aborting!" and "doing step 3..." printed.

我确定我只是误解了这里的一些基本原则,因此希望得到任何帮助.谢谢!

I'm sure I'm merely misunderstanding some basic principles here, so would appreciate any help. Thanks!

推荐答案

在 promise 链中抛出的任何错误都会导致整个堆栈提前中止,并将控制权交给错误返回路径.(在这种情况下,fail() 处理程序)当您检测到某个状态导致您想要中止承诺链时,只需抛出一个非常具体的错误,您将其捕获在错误返回中并忽略(如果您这样做)选择)

Any errors that are thrown within the promise chain will cause the entire stack to be aborted early and control is given to the error-back path. (in this case, the fail() handler) When you detect a certain state which causes you to want to abort the promise chain, then just throw a very specific error, which you trap in the error-back and ignore (if you so choose)

function doTask(task, callback)
{
    Q.ncall(task.step1, task)
    .then(function(result1){
        if(result1 == 'some failure state I want to cause abortion')
        {// the rest of the task chain is unnecessary 
            console.log('aborting!');
            throw new Error('abort promise chain');
            return null;
        }
        return Q.ncall(task.step2, task);
    })
    .then(function(result2){
        console.log('doing step 3...');
        return Q.ncall(task.step3, task);
    })
    .fail(function(err) {
        if (err.message === 'abort promise chain') {
            // just swallow error because chain was intentionally aborted
        }
        else {
            // else let the error bubble up because it's coming from somewhere else
            throw err;
        } 
    })
    .end();
}

这篇关于如何使用 Q 正确中止 node.js 承诺链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆