, callback and return, good practice

Hi there. I’m currently new in meteor, and to be honest, i’m not pretty experienced into javascript as well :slightly_smiling:

So I come here to get an advice about the best practice to get call method on the server, getting the callback etc…

Here is an example on i’m pretty stuck :

client.js : This is just a simple event listenner on a button’s click{
    'click #myBtn': function (e, obj) {"processRequest", function (err, data) {
                console.log("err : " + err);
                console.log("data : " + data);

server.js :

var http = Npm.require("http");
    "processRequest": function () {
        var response = {};  // return var

        // option http who seems like this : http://localhost:8091/telnet/?type=users
        var options = {   
            host: 'localhost',
            port: 8091,
            path: '/telnet/?type=users',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'

        // asynchronous http request 
        var prot = http;
        var req = prot.request(options, function(res)
            var output = '';
            console.log( + ':' + res.statusCode);

            res.on('data', function (chunk) {
                output += chunk;

            return res.on('end', function(err) {
                var obj = JSON.parse(output); // my json seems like : {"threads":[],"type":"text"}
                return obj; // my obj seems like : {array[0] threads, "type"="text"}
                // until there... all is fine !

        req.on('error', function(err) {
            res.send('error: ' + err.message);


SO… when I’m getting my request’s result in the res.on code part, obviously, asynchronously (:slightly_smiling:) the result i get on the client side is data = undefined.
I tried to separate all my code into different functions to do like “true” javascript, to emule synchronous call and getting my result properly… but it’s still doesn’t works !

How can I do this clearly in meteor ?
Thx for your help, ideas, discussion about :stuck_out_tongue:

Why asynchronous? On the server we can do this synchronously (check the HTTP docs).

I’ve probably missed something, but I have pared down your method to this:

  "processRequest": function() {
    var url = 'localhost:8091/telnet/?type=users';

    // synchronous http request 
    try {
      return HTTP.get(url).data;
    } catch (err) {
      throw new Meteor.Error(err);

You will need to meteor add http to your project. You do not need Npm.require.

1 Like

Thx Rob for your answer.

Yep, this is working by this way, and i’m agree with you, this HTTP is better than this Npm.require.

I know, in this case I can passed by synchronous, but the reason i passed asynchronously is I’m often in front of this kind of problem and I never know how to resolve it clearly. (I mean, 1 asynchronous with 1 callback = ok, 2 = hard… 3 = blowing my mind)
I tried to used bluebird or even Fiber to get out of this situation but I am not satisfied.

I just found wrapAsync solution and this video who explain how it works… and this is for me the real solution. I’ll study this deeper soon, it sounds interresting !

Thanks for your answer anyway, and I’m still interrested by others solutions for asynchronous callbacks !! :slightly_smiling:

great post. Solved the same problem I had (need to keep the server http call synchronous)

1 Like