Meteor.js - how to check values ​​from an asynchronous callback

CONTEXT

Im makes a call, which if successful, changes the boolean value from false to true. Then, outside this call, I check to see if this is a boolean, and if so, I go to another page.

PROBLEM

Console logs indicate that the if statement, which checks the boolean, is executed before the calls have time to change the boolean. I understand that this is due to asynchrony, but I'm not sure what the correct design template will be for this. Here is a snippet:

     //set variables to check if the even and user get updated or if error
    var eventUpdated = false;

     Meteor.call('updateEvent', eventId, eventParams, function(error, result){
      if(error){
        toastr.error(error.reason)
      } else {
        var venueId = result;
        toastr.success('Event Info Updated');  
        eventUpdated = true;

        console.log(eventUpdated)
      }               
    });

    console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

POSSIBLE SOLUTIONS

, , if , . Googling, , , , .

+4
2

, , , , . :

    Tracker.autorun(function(){
        if (Session.get('userUpdated') && Session.get('passwordUpdated') && Session.get('eventUpdated')) {
            Router.go('/get-started/confirmation');
        }
    });

.

+2

. , , :

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

. Session.set :

Session.set("eventUpdated", "true");

:

eventUpdated = Session.get("eventUpdated");

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

Session , .

+1

Source: https://habr.com/ru/post/1623177/


All Articles