【发布时间】:2022-01-03 01:42:43
【问题描述】:
在使用await 的异步函数内部使用Promise.prototype.catch 有什么影响吗?
例如:
async funciton sendMessage(topic, bodyObj) {
// implementation is not important
}
async function removeFromInventory(item, amount) {
const storeData = await getSoreData(item)
const removeResult = await inventory.removeFromStore(storeData.id, item.id, amount)
sendMessage(
Message.REMOVE_INVENTORY_SUCCESS, removeResult
).catch(log.error) // <== here, is it has downside and it is better to use try/catch?
return removeResult
}
我不想 await 完成 sendMessage 并使用 .catch 处理拒绝,换句话说,避免未处理的拒绝。
【问题讨论】:
-
这意味着它不再是同一个承诺链的一部分,但听起来这就是你正在寻找的特定行为。
-
是的,你可以,但发明 async await 的原因之一是避免使用 catch,而是使用 try catch
-
@Ivandez 然后我需要等待它,这正是我想要避免的
标签: javascript async-await es6-promise