1

I have the below handleSubmit function which makes 3 calls for the handleRequest async function (common function used to fetch result). I want to wait until all 3 functions finishes execution and need to show a success message if all 03 executes successfully. How can I identify when the 03 executions finishes programatically?

const handleSubmit = () => {

    if (isTravelStatusChanged) {
        const body = {
            travel_id: xxx,
            status: xxx
        }

        handleRequest(editTravelStatus, "PUT", body, (data) => {
            let affectedRowCount = data.affectedRowCount ? data.affectedRowCount : 0
            if (affectedRowCount > 0) {
                console.log("Result 1")
            }
            else {
                setSnackbarData({ message: "Failed", open: true, severity: "error", onClose: onCloseSnackbar })

            }
        })

    }

    if (isTravelAgentChanged) {
        const body = {
            travel_id: xxx,
            travel_agent_id: xxx
        }

        handleRequest(editTravelAgent, "PUT", body, (data) => {
            let affectedRowCount = data.affectedRowCount ? data.affectedRowCount : 0
            if (affectedRowCount > 0) {
                console.log("Result 2")
            }
            else {
                setSnackbarData({ message: "Failed", open: true, severity: "error", onClose: onCloseSnackbar })

            }
        })
    }

    if(isVisaChanged) {
        const body = {
            travel_id: xxx,
            visa_id: xxx
        }

        handleRequest(editVisa, "PUT", body, (data) => {
            let affectedRowCount = data.affectedRowCount ? data.affectedRowCount : 0
            if (affectedRowCount > 0) {
                console.log("Result 3")
            }
            else {
                setSnackbarData({ message: "Failed", open: true, severity: "error", onClose: onCloseSnackbar })

            }
        })
    }

}
shayanmalinda
  • 1,925
  • 3
  • 12
  • 23

0 Answers0