Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i execute a code only after the promise is done

every thing work in my code, the problem is that this.loading=false run before the for() is done sending all the requests.

How can make this.loading=false get executed only after the for() finishes sending all the requests ?
i hope you got the idea.

methods:{
    fileChange(event){
        this.loading=true
        //code
        for(var i=0; i < tasks.length; i++){
            //code
            axios.post('/api/operation', data)
                .then(response=>{
                    this.operations.push(name:response.data.name});
                })
                
        }
        
        //i want to the bellow code to be executed after for() is finnished!
        this.loading=false
    },

},

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use async/await

methods: {
    async fileChange(event) {
      this.loading = true
      for (let i = 0; i < tasks.length; i++) {
        const {data} = await axios.post('/api/operation', data) // << whats data?
        
        this.operations.push({ name: data.name })
      }
      this.loading = false
    },
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading