简体   繁体   English

在 Node.js 中嵌套承诺的情况下承诺解决/拒绝

[英]Promise resolve/reject in case of nested promise in Node.js

In case of an error thrown by a promise which is called within parent promise should we catch the error or will it be caught automatically?如果在父承诺中调用的承诺引发错误,我们应该捕获错误还是会自动捕获错误?

Example:例子:

function p1(val)
{
    return new Promise((resolve, reject) => {

        //do something with val
        if(val == true)
           reject(err);

        p2()
        .then(result => resolve(result)
        .catch(reject); //is this line necessary?
    });
}

function p2()
{
    return new Promise((resolve, reject) => {
        //resolve or reject...
    });
}

With your revised code, where you're doing work prior to calling p2 , you have a few options.使用修改后的代码,您在调用p2之前所做的工作,您有几个选择。 If you're happy for errors in the initial, synchronous part of your functino to be synchronous errors rather than promise rejections, you could just do this:如果您对 functino 的初始同步部分中的错误是同步错误而不是承诺拒绝感到高兴,您可以这样做:

function p1(val) {
    //do something with val
    if (val == true)
        throw err;

    return p2();
}

The first part of that happens synchronously, then returns the promise from p2 .第一部分是同步发生的,然后从p2返回承诺。 Whether you do this is partially down to what the function is doing and partially down to style.你是否这样做部分取决于函数正在做什么,部分取决于样式。 If the initial synchronous part is setting up an asynchronous operation, and you want to have the function throw (rather than return a rejected promise) when it has a problem setting up the asynchronous process (then fulfill/reject based on whether the asynchronous process worked), you might do it like this.如果初始同步部分正在设置异步操作,并且您希望函数在设置异步进程出现问题时抛出(而不是返回被拒绝的承诺)(然后根据异步进程是否工作完成/拒绝) ),你可以这样做。

If you want the function to always report success/failure via a promise, then if you're doing initial work, you do need your own promise:如果您希望函数始终通过承诺报告成功/失败,那么如果您正在进行初始工作,您确实需要自己的承诺:

function p1(val) {
    return new Promise((resolve, reject) => {
        //do something with val
        if(val == true)
           reject(err);

        resolve(p2());
    });
}

The resolve(p2()) part resolves the promise p1 created to the promise from p2 : If p2 's promise rejects, p1 's promise rejects with the p2 rejection reason; resolve(p2())部分将创建的承诺p1解析为来自p2的承诺:如果p2的承诺拒绝,则p1的承诺以p2拒绝原因拒绝; if p2 's promise fulfills, p1 's promise fulfills with the p2 fulfillment value.如果p2的承诺履行, p1的承诺履行与p2履行价值。

Or you might use an async function, which has the same result:或者您可以使用async函数,它具有相同的结果:

async function p1(val) {
    //do something with val
    if(val == true)
        throw err;

    return p2();
}

In both cases, that ensures any error thrown by the initial code prior to p2 results in a rejection rather than a synchronous error, even if the initial code doesn't involve asynchronous processing.在这两种情况下,这确保了p2之前的初始代码抛出的任何错误都会导致拒绝而不是同步错误,即使初始代码不涉及异步处理。

The main thing to remember is that when you already have a promise (such as the one from p2 ), there's no need to use new Promise ;要记住的主要事情是,当您已经有一个承诺(例如来自p2的承诺)时,没有必要使用new Promise instead, just chain off the promise you already have.相反,只需将您已有的承诺联系起来即可。 ( More here .) But when you're doing something before you get the promise, as in your revised example, you might create your own depending on whether you want the first part of your function's errors to be synchronous or promise rejections. 更多信息在这里。)但是当你在得到承诺之前做一些事情时,就像在你修改后的例子中一样,你可以根据你是希望函数错误的第一部分是同步的还是承诺拒绝来创建自己的。

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

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