Hey guys!
I am a bit new to using Meteor and am looking for advice on appropriate ways to write synchronous methods on the server. If I say something that is incorrect at any point, please do not hesitate to correct me. Here is a short example to help get the conversation going. Say I have a method in Meteor.methods that needs to have a synchronous flow:
Meteor.methods({
someMethod: function() {
computeTwoPlusTwo(); // Prints 4 to the console
callForInfoSomeRestAPI(someVar, function(result, err) {
if (err) throw err;
console.log(result);
});
computeTwoPlusTwo();
}
});
I would like the output of my console to be:
4
The result of callForInfoSomeRestAPI
4
What would be the best way to guarantee this result?
From my understanding, methods on the server are synchronous (or run in sequence) because they run in a fiber. But in my example above, the callback of callForInfoSomeRestAPI would be executed only when it receives a response meaning the output I would like could be out of order. The internet says I can solve this with Meteor.wrapAsync. If I wrap callForInfoSomeRestAPI with this method, it would block until the callback has been executed.
Additionally, when is it appropriate to run code in a new fiber? I have found myself querying the database on new fibers in order to get some of my server code working.
Thanks so much!