【发布时间】:2019-07-08 12:05:34
【问题描述】:
我在新函数中创建 async/await(try...catch)。
我在另一个组件中调用这个函数。 然后,我只想得到返回错误值并显示错误值。
我该怎么做?
我的功能:
const updateInformation = async () => {
try {
const response = await updateAPI({
variables: {
data: {
name: state.name,
phoneNumber: state.phoneNumber,
},
},
})
} catch (ex) {
return Utils.extractErrorMessage(ex)
}
}
组件:
const onPressSendButton = () => {
if (name && address1 && address2 && phoneNum) {
const r = updateInformation()
// I want to show return error value in this line.
} else {
return false
}
}
【问题讨论】:
-
什么是
updateAPI表示您正在使用fetch或其他什么? -
@ravibagul91
updateAPI是突变(阿波罗)。 -
不知何故,你没有像我猜的那样使用这个异步函数。因为异步函数总是返回一个
promise。因此,无论是在普通块中还是在error块中,如果您返回某些东西,您就会将其作为承诺解决。那么,如果只解决错误是您的真正意图,您在哪里检查updateAPI是否在您的代码中引发错误?
标签: javascript reactjs react-native