Chiko
Chiko

Reputation: 2340

How to halt execution in Vuejs until get request is finished

I have this code requesting data from an api endpoint:

fetchStudentMeta: function(){
    var vm = this;
    this.$http.get('/api/1.0/students/metadata/ '+ this.selectedStudent.id)
        .then(function (response){
            this.$set('meta', response.data);
            console.log(vm.meta); //line1
        });
    console.log(this.meta); //line2
},

In my console and execution, line 2 is processed before line 1. Any way to halt execution until my variable is set from the get request data?

Upvotes: 1

Views: 1304

Answers (1)

Rifki
Rifki

Reputation: 3530

The first line is placed in then() closure which means will not be processed until the request is done while the second line is processed immediately once the request initiated.

The question is what's actually you're trying to do with this.meta attribute (line 2)? if you intend to cast it you can use computed, or you can $dispatch() or $broadcast() to trigger an event.

Upvotes: 1

Related Questions