【发布时间】:2019-07-09 02:16:21
【问题描述】:
在下面的代码中,我正在尝试捕捉。
try-catch 功能:
const updateReturnInformation = async () => {
if (state.isLoading) {
throw new Error('Errrrrror!!!') // <- It is success working
}
dispatch(actionSetLoading(true))
try {
await updateReturnAPI(...)
} catch (ex) {
return ex // <- why not working?
} finally {
dispatch(actionSetLoading(false))
}
}
而且,我在另一个组件中调用了这个函数:
...
try {
await updateReturnInformation()
navigation.navigate(Routes.Root.Modal.Confirm, {
title: 'success!',
buttons: ['OK'],
})
} catch (ex) {
navigation.navigate(Routes.Root.Modal.Confirm, {
heading: 'Error',
title: ex,
buttons: ['OK'],
})
}
...
我调用了 updateReturnInformation(),但它只能打印“成功!”消息。
此外,console.log 仍然只会打印“成功!”发生错误时的消息。
将 updateReturnInformation () 的 return error 更改为 throw error 可以正常工作,但我只能使用 return error。
怎么了?
【问题讨论】:
-
您没有将
updateReturnInformation()的返回值分配给任何东西。你怎么知道return ex不起作用?
标签: javascript