Delay problem of vue for Loop calling axios to create a new array based on the returned data

          let mindex = 0 ;
          let sum = this.multipleSelection.length ;
          let successNum = 0 ;

          for(const item of this.multipleSelection){
            jokeNumPP
            axios
              .get(MODULE_PATH.GAL_PATH + "/init-rule/init",{
                params:{
                  admCode:item.admCode,
                  eleCode:item.eleCode,
                  fiscal:this.fiscal
                }
              })
              .then(({ data,code }) => {
                flag = true
                if(code=="200"){
                  successNumPP  
                  console.log(successNum)   // 
                }else{
                  failNumPP;
                  console.log(failNum)
                }
              })
              .catch((err) => {
                this.$message({
                  type: "error",
                  message: err.msg
                });
              });
          }
          console.log(jokeNum)
          console.log(flag)
          console.log(this.successNum)     //
The

code, as above, makes a request in a for loop, and then saves the eligible data to a new variable according to the returned data, but at present, after the end of the for loop, the new variable is printed first, and the new variable is still empty. In the for loop condition judgment, it is the last print, asking for advice on how to get the correct new variable after the end of the loop?

Dec.06,2021

learn about the concurrency of multiple requests axios.all ()


this is an asynchronous request, you can learn about Event Loop
about this scenario, you can use

  

async/await learn about

Menu