How to prevent autorun rerunning while setting reactive variable in server response

Tracker.autorun(function({
        var x = Session.get('x');
        Meteor.call('server' ,x, function(err, res){
              if(res){
                    Session.set('x', 5);
              }
       });
});

You should never get and set the same reactive variable in the same reactive context. Here’s what happens:

  1. Get x (this establishes a reactive dependency on x).
  2. Do some stuff
  3. Set x (this invalidates the dependency and re-runs from 1.)

Basically, you have written a reactive infinite loop.

4 Likes