How can i block program before getting meteor.call() callback res?


#1

Template.quote.helpers({
bid:function()
{
var bit;
Meteor.call(‘getbid’,code,function(err,res){
bit = res;
});
return bit;
}
})

I want to return the result of the callback function, but before the result back, the undefined bit will be returned. how to return the value after getting the callback result?


#2

is this a one time call per template instance, or is it bound to change during the lifetime of the template?

if it’s just a single occurrence, I’d move the method call to the Template.quote.created event, and communicate the result to the helper using a reactive variable:

Template.quote.created = function() { 
          var instance = this;         
          instance.currentBid = new ReactiveVar;
         var code = this.data.code; // assuming you get the code in the data context
         Meteor.call('getbid', code, function(err, res) {
             instance.currentBid.set(res);
        });
 }

Template.quote.helpers({
  bid: function() { 
       return Template.instance().currentBid.get(); 
 } 
});

quite horrible, but that’s where Meteor falls short.


#3

it works. but it should add reactive-var package first.

However, for my case, the ‘code’ is come from another meteor.call()


Meteor.call(‘getcode’,id,function(err,res){ //id is known variable
code = res;
});

so I wonder how let the method ‘getbid’ executing after getting the result from method ‘getcode’ ?


#4

if your async calls are becoming more complex and involved, there are 2 things you may consider:

  1. using Promises to organize them. in this specific case, you would use a serial pattern, i.e. one promise acting upon the completion of a previous one

  2. if possible, perhaps there’s reason to refactor the 2 methods into 1

in either case, this issue does not necessarily concern Meteor as a platform, as Meteor has been relatively agnostic about this so far


#5

you can try ReactiveMethod package.