1

this is my code:

var timeNow = function () {
    var time = new Date();
    return ((time.getHours() < 10)?"0":"") + time.getHours() +":"+ ((time.getMinutes() < 10)?"0":"") + time.getMinutes() +":"+ ((time.getSeconds() < 10)?"0":"") + time.getSeconds();
};

var RegisterConnection = function (socketid, AuthToken) {
    request.get("api.php", function(err, res, Response){ //Response will return {"result": true}
        Response = JSON.parse(Response);
        if (!err && res.statusCode === 200 && Response.result) {
            console.log(Response.result); // true on console
            return (String(Response.result));
        }
    });
};

io.sockets.on("connection", function (clientSocket) {
    clientSocket.on("connectionDone", function (data) {
        clientSocket.authToken = data.authToken;
        clientSocket.emit(
            "info", {
                Time: timeNow(), // It's printing the time
                information: RegisterConnection(data.socketid, data.authToken) // It's printing undefined
            }
        );
    });
});

I'm new to javascript closure, this is not working for me as RegisterConnection is returning undefined. But in the function itself, the console.log does print true in the console. What am I doing wrong?

Jin Wang
  • 254
  • 1
  • 7
  • 19
  • RegisterConnection has no return statement, so you'll always get undefined – Roberto Mar 17 '14 at 14:04
  • Thank you @Ian, I'm reading it now. If I find that answer fits, I will close this one. – Jin Wang Mar 17 '14 at 14:06
  • @rsc1975 it does have one return (String(Response.result)); Although it's in an if statement. That's because I've only included partial of my code. But let's assume the if condition is always true, it's still returning undefined. – Jin Wang Mar 17 '14 at 14:06
  • Hi @TheJinStudio, no, It has not a return statement, because that return is inside the request.get callback, It's another scope. In this case It could be a good option to use "promises" – Roberto Mar 17 '14 at 14:08
  • @TheJinStudio, I've added an answer as possible solution, maybe It could help you – Roberto Mar 17 '14 at 14:13

1 Answers1

2

Please, check if this approach works for you:

var timeNow = function () {
    var time = new Date();
    return ((time.getHours() < 10)?"0":"") + time.getHours() +":"+ ((time.getMinutes() < 10)?"0":"") + time.getMinutes() +":"+ ((time.getSeconds() < 10)?"0":"") + time.getSeconds();
};

var RegisterConnection = function (socketid, AuthToken, onSuccess) {
    request.get("api.php", function(err, res, Response){ //Response will return {"result": true}
        Response = JSON.parse(Response);
        if (!err && res.statusCode === 200 && Response.result) {
            console.log(Response.result); // true on console
            onSuccess(String(Response.result));
        }
    });
};

io.sockets.on("connection", function (clientSocket) {
    clientSocket.on("connectionDone", function (data) {
        clientSocket.authToken = data.authToken;
        RegisterConnection(data.socketid, data.authToken, function(result) {
            clientSocket.emit(
                "info", {
                    Time: timeNow(), // It's printing the time
                    information: result
                }
            );
        })
    });
});
Roberto
  • 8,586
  • 3
  • 42
  • 53
  • great :-) Anyway, If you have many callbacks inside other callbacks, you could take a look to promises, They help to deal with an async world in a tidy way, there are many articles about them, for instance: http://12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/ – Roberto Mar 17 '14 at 14:25