简体   繁体   English

如何将带有最终错误的Q Promise链接到finally()

[英]How to chain Q promise with previous errors on finally()

I am wondering how to chain errors on Q using finally. 我想知道如何最终使用Q链接错误。

Consider following code 考虑以下代码

function p1() {
    throw new Error("p1 error");
}

function p2() {
    throw new Error("p2 error");
}

function p3() {
    return Q.fcall(p1)
        .finally(function () {
            return Q.fcall(p2);
        }); 
}

p3()
    .done();

The Error with message "p1 error" is lost since it was override by Error "p2 error". 消息“ p1错误”的错误已丢失,因为已被错误“ p2错误”覆盖。 How can I throw all the errors (or combine the errors)? 如何抛出所有错误(或合并错误)?

Currently, I am working on a socket connection on nodejs. 目前,我正在nodejs上进行套接字连接。 I am using .finally() to close the socket after each connection. 我正在使用.finally()在每次连接后关闭套接字。

However, errors (eg: authentication error) before .finally() will be overriden by one in .finally() (eg: connection close error). 但是,.finally()之前的错误(例如:身份验证错误)将被.finally()中的一个错误覆盖(例如:连接关闭错误)。 So, I am wondering how to get all errors 所以,我想知道如何获取所有错误

Thanks 谢谢

You could do something like this: 您可以执行以下操作:

function p3() {
    return Q.fcall(p1)
    .finally(function(p) {
        return Q.fcall(p2).catch(function(e) {
            if (!p.isRejected()) throw e;
            var agg = new Error("multiple errors occured");
            agg[0] = p.inspect().reason;
            agg[1] = e;
            agg.length = 2;
            throw agg;
        });
    }); 
}

(based on Bluebird's AggregateError ) (基于Bluebird的AggregateError

Of course it is tedious, but I can't imagine something better. 当然这很乏味,但是我无法想象更好的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM