Http.request response event not fired


#1

I’m pulling my hair off on this callback crap and events not being fired. Basically, I have a helper object in global scope that I set in my main.js file:

var ElJS = require('./ElecJS/ElecJS.js')
global.ElecJS = new ElJS()

Within this helper object, I have a useful function to send http request:

this.Send = function(methodStr, addr, data, cb){
        var URL = require('url');
        urlObj = URL.parse(addr,true, true)
        if(data==null) data="{}"
        if(methodStr=="GET"){
            urlObj.query = data
            urlObj.search=null
        }
        if(urlObj.protocol==null){
            urlObj.protocol="http"
        }
        urlObj.slashes=true
        var respData=""
        var isFinished = false
        var req = http.request({
            hostname:"127.0.0.1",
            port: this.uiPort,
            method: methodStr,
            path: URL.format(urlObj)
            }, 
            cb
        );
        if(methodStr=="POST"){
            req.write(JSON.stringify(data))
        }
        req.end()
    }

And, I’m trying to use that feat in a menu-item where my click function is defined like so:

click: function(item, focusedWindow) {
           ElecJS.Send('GET', '//console/UIElem/tab/new',{},function(resp){
               console.log("menu_tab send callback is being called")
               resp.setEncoding('utf8');
               var data =""
               resp.on("data", function(d){
                   console.log("getting some data:", d)
                   data += d
               });
               resp.on("end",function(){
                   console.log("Final menu_tab data:", data)
               });
           });
       }

The end result is that when I click this bloody menu-item no ‘data’ event is fired. But when I click it a second time I suddenly get twice the data (from first and second click)

Oddly enough, when I call this ElecJS.Send function from a browser-window (renderer process), I get the http response from first click. Maybe that’s pure luck, but I couldn’t reproduce the above bug from renderer.

Yeah, I’m new to javascript and node…


#2

I’ve just found a trick to get the response and data events: I wrap the ElecJS.Send function in a setTimeout(cb ,0), like so:

click: function(item, focusedWindow) {
           setTimeout(function(){
               ElecJS.Send('GET', '//console/UIElem/tab/new',{},function(resp){
                   console.log("menu_tab send callback is being called")
                   resp.setEncoding('utf8');
                   var data =""
                   resp.on("data", function(d){
                       console.log("getting some data:", d)
                       data += d
                   });
                   resp.on("end",function(){
                       console.log("Final menu_tab data:", data)
                   });
               });
           },0)
       }

This is perfectly stupid but it works, and I have no idea why. Does anyone understand what’s happening?