Reputation: 75
For example, the following is a basic node http server (from their official site), and we are passing a callback to createServer()
. Is there any guarantee that the calls to foo()
and bar()
will be executed in that particular order, and the normal control flow will be maintained?
I know that in order to enqueue messages into a queue which is polled by the event loop, we must pass a callback function with foo()
and bar()
. But is it guaranteed that the event loop won't mess with the control flow if I call the functions the way I just did.
Note: I actually want to enforce this control flow (let's think of foo()
and bar()
as functions that will eventually do error checking on the incoming HTTP request without doing anything particularly I/O intensive (I am assuming console.log
isn't that I/O intensive, correct me if I am wrong)).
var http = require('http');
http.createServer(function (req, res) {
foo();
bar();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
function foo()
{
console.log('This is foo');
}
function bar()
{
console.log('This is bar');
}
Upvotes: 0
Views: 289
Reputation: 2080
In your example, foo()
and bar()
do not yield control of the event loop, and so they always get executed in your intended order, and before the res...
commands.
Upvotes: 3