首页 > 解决方案 > 查找 .then 链中哪个承诺失败

问题描述

我正在尝试在我的 node + express 应用程序中设计一个带有 catch 错误的承诺链。在下面的示例中,如果任何一个“then”函数出错,我将不得不向后工作以从错误消息中找到哪一个。有没有更简单的方法来编写 catch 函数?

    new Promise(function(resolve, reject) {
       resolve(groupId);
    })
    .then(sid => getGuestGroup2(sid))matching carts
    .then(group =>getProducts(group))//add products
    .then(result2 =>getPrices(result2))
     .catch(error => { // (**)
        console.log('Error on GET cart.js: '+error);
        res.sendStatus(500);
      });

标签: javascriptnode.jsexpresspromisees6-promise

解决方案


Promise链接足够通用,不包含开箱即用的“哪个步骤失败”类型的信息。您可能会尝试从堆栈跟踪中对其进行解码,但在我看来,这比它的价值要多得多。您可以使用以下几个选项来确定哪个步骤失败。

选项1

在错误对象上设置一个额外的属性(作为指示符),可以在 catch 块中对其进行解码,以确定错误源自链中的哪个步骤。

function predictibleErrors(promise, errorCode) {
    return Promise.resolve(promise)
        .catch(err => {
            const newErr = new Error(err.message);
            newErr.origErr = err;
            newErr.errorCode = errorCode;
            throw newErr;
        });
}

Promise.resolve(groupId)
    .then(sid => predictibleErrors(getGuestGroup2(sid), 'STEP_1')) // matching carts
    .then(group => predictibleErrors(getProducts(group), 'STEP_2')) // add products
    .then(result2 => predictibleErrors(getPrices(result2), 'STEP_3'))
     .catch(error => { // (**)
        console.log('Error on GET cart.js: '+error);

        // Check error.errorCode here to know which step failed.

        res.sendStatus(500);
      });

选项 2

好老,每一步都抓住,然后重新抛出以跳过后续步骤。

Promise.resolve(groupId)
    .then(sid => getGuestGroup2(sid)) // matching carts
    .catch(err => {
        console.log('step 1 failed');
        err.handled = true; // Assuming err wasn't a primitive type.
        throw err;
    })
    .then(group => getProducts(group)) // add products
    .catch(err => {
        if(err.handled) { return; }
        console.log('step 2 failed');
        err.handled = true;
        throw err;
    })
    .then(result2 => getPrices(result2))
    .catch(err => {
        if(err.handled) { return; }
        console.log('step 3 failed');
        err.handled = true;
        throw err;
    })
    .catch(error => {
        // Some step has failed before. We don't know about it here,
        // but requisite processing for each failure could have been done
        // in one of the previous catch blocks.
 
        console.log('Error on GET cart.js: '+error);
        res.sendStatus(500);
    });

请注意,我们在选项二中所做的事情,也可以直接通过底层方法完成。例如。getGuestGroup2或者可以在它抛出的错误对象上getProducts包含一个或类似的属性。errorCode在这个例子中,我们知道第 1 步或第 2 步失败,但不知道为什么。如果底层方法设置相同,它们可以包含更准确的错误代码,并知道操作失败的原因。我没有采取那条路线,因为这些方法不包含在您的样本中,而且据我所知,它们可能不在您的控制范围内。


推荐阅读