I need a way for a meteor call to be synchronous so that when a call is run, the code waits for a result to be done so it can continue to the next line of code on the client.
for example:
clientFunction = function(){
Meteor.call('serverFunction', function(err,result){})//<--so when this gets a result and
//is stored in a session variable
var doSomeThing = Session.get('whatever') <-- so that your able to use it here
}
I've tried using a while loop to prevent anything from happening until a value is returned, but it seems like it runs after the clientFunction thus throwing it to its demise
any help would be appreciated
This is a very common question, being asked in various shapes and forms. Most people don't realize when they are making asynchronous calls. The solution, however, is always the same: wrap your method code on the server into a fiber or use a future.
The best practice, I think, is to use the currently available Meteor._wrapAsync
function as described, e.g., here:
Meteor: Calling an asynchronous function inside a Meteor.method and returning the result
Some other options are described here: https://gist.github.com/possibilities/3443021
Update: The method is now called Meteor.wrapAsync
.