我想在一个函数完成后运行一个函数,并将一些数据传递给第二个函数。 我分别测试了这两个功能,它们都有效。但我想在第一个完成后立即启动第二个
这是我的代码,但第二个函数无法正常运行。
const ChargeCard = (dispatch) => ({
cardNumber,
expiryMonth,
expiryYear,
cvc,
email,
amountInKobo,
id
}) => {
try {
RNPaystack.chargeCard({
cardNumber: cardNumber,
expiryMonth: expiryMonth,
expiryYear: expiryYear,
cvc: cvc,
email: email,
amountInKobo: amountInKobo * 100
})
.then(response => {
if (response) {
setBasic({ id }) //the function to run if this is successful
}
})
} catch {
dispatch({
type: "ADD_ERROR",
payload:
"Something went wrong during payment, please try again, don't worry you have not been charged."
});
}
};
这个函数我想立即运行即成功;
const setBasic = () => async ({ id }) => {
const Basic = 1
const response = await appApi.put(`/usersub/${id}`, { Basic });
if (response) {
console.log(response.data)
}
}
最佳答案
将第二个函数更改为:
const setBasic = async ({ id }) => {
const Basic = 1
const response = await appApi.put(`/usersub/${id}`, { Basic });
if (response) {
console.log(response.data)
}
}
您将其定义为:
const setBasic = () => async ({ id }) => {
但是你用以下方式调用它:
setBasic({ id })
关于javascript - 在 React Native 中函数成功后,如何立即启动新函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60284305/