功能成功响应本机后,如何立即启动新功能

时间:2020-02-18 15:27:27

标签: javascript react-native ecmascript-6

我想在一个函数完成后立即运行一个函数,并将一些数据传递给第二个函数。 我已经分别测试了这两个功能,它们都可以工作。但是我想在第一个完成后立即启动第二个

这是我的下面代码,但是第二个功能无法正常运行。

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)
    }
}

2 个答案:

答案 0 :(得分:1)

将第二个功能更改为此:

var myModel = (await httpReq.ReadFormAsync()).BindToModel<MyModel>();

您已将其定义为:

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 }) => {

答案 1 :(得分:1)

为什么将async awaitthen()混合在一起并坚持下去。

const setBasic = async ({ id }) => {
    const Basic = 1
    const response = await appApi.put(`/usersub/${id}`, { Basic });
    if (response) {
        console.log(response.data)
    }
}

const ChargeCard = (dispatch) => async ({
    cardNumber,
    expiryMonth,
    expiryYear,
    cvc,
    email,
    amountInKobo,
    id
}) => {

    try {
        const response = await RNPaystack.chargeCard({
            cardNumber: cardNumber,
            expiryMonth: expiryMonth,
            expiryYear: expiryYear,
            cvc: cvc,
            email: email,
            amountInKobo: amountInKobo * 100

        })
        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."
        });    
    }

};

这样,您的登录名是一致的。但是,我只想提一下。您的第一个函数看起来像是写action creators using redux-thunk的简便方法,以防万一我没错,我认为您在第一个函数中错误地传递了dispatch。您应该将函数重写为此:

const ChargeCard = ({
    cardNumber,
    expiryMonth,
    expiryYear,
    cvc,
    email,
    amountInKobo,
    id
}) => async dispatch => {

    try {
        const response = await RNPaystack.chargeCard({
            cardNumber: cardNumber,
            expiryMonth: expiryMonth,
            expiryYear: expiryYear,
            cvc: cvc,
            email: email,
            amountInKobo: amountInKobo * 100

        })
        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."
        });    
    }

};