Make Meteor Method Synchronous

I am trying to make this function synchronous. I read a few stack overflow messages about Async, but I can't figure out how I can do this synchronously. It is asynchronous at the moment, so it returns undefined before going into the callback function.

I call it on the client side:

Meteor.call('screenName',function(error,result) { if (error) { console.log(error); } else { window.alert(result); } } 

And this is the server side method:

 Meteor.methods({ 'screenName': function() { T.get('search/tweets', { q:'#UCLA', count:1 }, function(err,data,response) { var temp = data.statuses[0].user.screen_name; console.log(temp); return temp; } ) } }); 

I am using the Twitter API, and what I want to do is basically extract the screen name from JSON and return it a client-side variable. But this returns undefined because the callback compiles after the compiler reaches the end of the screenName function.

I want it to return a value from a callback function, but reading other examples did not help me understand how I can convert my code. I need to make this function synchronous, but I don't know how to do it.

+6
source share
4 answers

Just use Meteor.wrapAsync to turn the asynchronous T.get into synchronously styled!

In fact, it will not be executed in a clean "synchronous" way, but it uses a trick known as Fiber, but you have to read the documents to find out more.
Here:

 var Tget = Meteor.wrapAsync(T.get); Meteor.methods({ 'screenName': function() { return Tget({ q : '#UCLA', count : 1 }).status[0].user.screen_name; } }); 
+7
source

Browser server, it is not possible to call methods synchronously. You are stuck in a callback code code. Synchronous calls are only possible on the server.

http://docs.meteor.com/#/basic/Meteor-call

On the client

The methods called by the client are executed asynchronously, so you need to pass a callback to watch the result of the call. The callback will be called by two arguments, an error and a result. The error argument will be null if no exception was thrown. When an exception is thrown, the error argument is an instance of Meteor.Error, and the result argument is undefined.

On server

On the server, you do not need to pass a callback - the method call will simply be blocked until the method is completed, returns a result or throws an exception, as if you were calling the function directly.

However, the synchronous style code for the server depends if everything else is synchronous. You might want to read the API docs if you can synchronously write a Twitter API request. Otherwise, you will write asynchronous code at the end.

+6
source

@thatgibbyguy's answer is the only one that worked for me. I also tried the following libraries (maybe they will be useful for someone):

 meteor add simple:reactive-method meteor add mnmtanish:call 
0
source

This is not synchronous code, but what I did to send the result of the server method back to the client function is to use serveression to do this. For instance:

 Meteor.methods({ 'screenName': function() { T.get('search/tweets', { q:'#UCLA', count:1 }, function(err,data,response) { var temp = data.statuses[0].user.screen_name; ServerSession.set('screenname',temp) return temp; } ) } }); 

Now you can set a reactive variable or session or template helper:

 ServerSession.get('screenname') 
-1
source

All Articles