我在新函数中创建 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
}
}
最佳答案
我做了一些更改,这应该有效(带有解释注释):
函数
const updateInformation = async () => {
return updateAPI({ // Missing return
variables: {
data: {
name: state.name,
phoneNumber: state.phoneNumber,
},
},
});
};
组件
const onPressSendButton = async () => {
if (name && address1 && address2 && phoneNum) {
try {
const r = await updateInformation(); // Need to await here
} catch (e) {
// This is where you handle the error
}
} else {
return false;
}
};
关于javascript - 如何获取函数的返回错误值? ( react native ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56934568/