javascript - 查找 .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);
});
解决方案
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 步失败,但不知道为什么。如果底层方法设置相同,它们可以包含更准确的错误代码,并知道操作失败的原因。我没有采取那条路线,因为这些方法不包含在您的样本中,而且据我所知,它们可能不在您的控制范围内。
推荐阅读
- optuna - Optuna 返回最后一个值,而不是一次试验的最佳值
- azure - 我正在 Azure DevOps 上创建用于部署 APIM APP 的发布管道。我的发布管道中有任务:AzureBlob 文件复制任务
- python - __setattr__ 禁止更改实例/自变量?
- c++ - TCP 丢包 (ns3)
- sql-server - 如何根据条件重置累计和?
- docusignapi - DocuSign Webhooks 响应 - 时区
- reactjs - 反应中的内部搜索栏
- firebase - 我想在flutter中创建一个带有firebase的listview,它将显示标题副标题和图像,当用户单击listview时将打开一个webview
- i18next - 斯巴达克斯和 i18n 复数规则
- javascript - 如何使用 ajax 将图像上传到服务器目录?