Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are callback functions executed in node.js?

I've just started learning node.js and express and there's something that confuses me a bit in the "hello world" example on the express.js website. In the example, they refer to the server variable inside the callback function.

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('App listening at http://%s:%s', host, port);
});

Does app.listen() return a value to server variable before it executes the callback function? How can it do that and how does it work? Is this the same for all callback functions in node (and javascript)?

I would just like a simple explanation of the execution process.

To be clear, I understand that the callback function has access to the server variable. But if the app.listen method executes the callback function before it returns a value to the server variable, wouldn't that mean that the server variable is still underfined when you try to access server.adress()? That is what I don't understand.

like image 267
Mattias Avatar asked Oct 19 '22 05:10

Mattias


2 Answers

Does app.listen() return a value to server variable before it executes the callback function?

Yes, exactly. app.listen() resembles to the plain Node.js server.listen method. The callback is an shortcut for assigning the server an listener to the listening event.

You could do the same with the following code:

var server = app.listen( 3000 );
server.on( "listening", function () {
  console.log( "server is listening in port 3000" );
});

How can it do that and how does it work? Is this the same for all callback functions in node (and javascript)?

This happens because IO events in Node.js are all run asynchronously (with exceptions from the fs module) - this is, they will only take place when other synchronous code have finished to run.

This works the same in browser JS - if you run some JS process synchronously, any events triggered (like click, blur, etc) will only execute after that one finishes.

like image 161
gustavohenke Avatar answered Oct 22 '22 00:10

gustavohenke


A function has access to all the variables that existed in the scope where it was created (unless it masks them).

var in_the_global_scope = 1;
function outer() {
    function inner() {
        alert(in_the_global_scope);
    }
    inner();
}

inner has access to any variable declared in inner, outer and the global scope.

A function being a callback isn't really relevant.

The listen method itself doesn't have access to server because listen was created in a different scope.

But if it returns a value, how can it then execute the callback function?

Because it doesn't just execute the callback. It waits for an event and the callback gets fired in reaction to that.

var timeOutId = setTimeout(function() {
  alert(timeOutId);
}, 1000);
like image 38
Quentin Avatar answered Oct 21 '22 23:10

Quentin