How to use vuex in vue to manage asynchronous requests so that all asynchronous requests in an event are executed before performing the next step

1. I have created a new store and want to manage an event with a status value:

import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

// 
const state = {
    status: ""
}

//  mutations
const mutations = {
    REQUESTWAITING(state) {
        console.log("waiting")
        console.log(state);
        state.status="waiting";
    },
    REQUESTSUCCESS(state) {
        console.log("success")
        console.log(state);
        state.status="success";
    },
    REQUESTERROR(state){
        state.status="error";
    }
}

const getters={
     getStatus(state)  {
        return state.status
    }
}


const actions={
    requestWaiting({commit})  {
        commit("REQUESTWAITING")
    },
    requestSuccess({commit}) {
        commit("REQUESTSUCCESS")
    },

    requestError({commit}){
        commit("REQUESTERROR")
    }
}


//  store 
export default new Vuex.Store({
    actions,
    getters,
    state,
    mutations
})

2: call vuex, in an event. Change the status of state to waiting, before entering the request. After the request is executed, if the status is successfully changed to success, and failed to change to error; (all these operations have been implemented). However, when I obtain the status value in the following method, I want to determine whether the request is completed, but I do not get the desired result. The value of status gets the value of waiting instead of the value after the callback. Is there any way to manage asynchronous requests so that all requests in an event are executed before performing the next step

        wFBtn_Next_fn:function(){
            wFConfig.wf_error=0;
            //
            wFeventBus.$emit("wFBtn_Next_event",event);
            let status=this.$store.state.status;
            console.log("status:")
            console.log(status);


                if(status=="error"){
                    console.log("");
                    return;
                }
                if(status=="success"){
                    console.log("")
                   return;
                }
            if(status!="success"){
                console.log("")
                return;
            }
            

clipboard.png

Mar.06,2021
promise in

ES6 handles asynchronous requests. It has a method: promise.all () executes the callback function when all asynchronous requests are successful, Promise.all ([p1, p2, p3]). Then (() = > {this.$store.dispatch ('requestSuccess')}) .
take a look at Ruan Yifeng's

use Promise and async/await

Menu