I have a question about setTimout
.
To understand my point let's take an example:
function f(callback)
{
var i = 0;
while(true)
{
++i;
if(i == 2000000000) break;
}
callback();
}
function g()
{
f(function(){console.log("CALLBACK");});
console.log("FIRST HERE");
}
g();
console.log("SECOND HERE");
So here as we expect, (even if the code has the asynchronous style with the callback
in the function f
), the while loop in f
is first executed, "CALLBACK" is printed, and THEN "FIRST HERE" is printed, followed by "SECOND HERE". This code behaves in a synchronous way even if at the first glance we can think that is asynchronous due to the callback
function passed in f
.
Now let's look at another example:
function f(callback)
{
setTimeout(function(){
var i = 0;
while(true)
{
++i;
if(i == 2000000000) break;
}
callback();
});
}
function g()
{
f(function(){console.log("CALLBACK");});
var i = 0;
while(true)
{
++i;
if(i == 2000000000) break;
}
console.log("FIRST HERE");
}
g();
console.log("SECOND HERE");
Now the code inside f
is encapsulated inside a setTimeout
. And here everything changes. setTimout
has not second argument for the time to wait. So normally the code should be executed now (as soon as possible). But here "FIRST HERE" and "SECOND HERE" are printed first and only AFTER that (after all the while loop execution in f
), the code inside the setTimout
is executed and then "CALLBACK" is printed. So we can observe that even if the code inside f
should be executed NOW, it is only at the end of the execution of the "main" code that the block (inside setTimout
) is executed.
So here my question. How javascript handles setTimeout
or setInterval
? In that example we observed that the code is not executed NOW (or a certain time specified in the second argument). It is executed only after javascript has some free time (after the execution of the main code). So how javascript implements that behaviour? There is maybe a priority stack (priority on the second argument of setTimout
) that keeps in memory the functions that have to be called in setTimout
. And as soon as javascript has free time, he deduced the time to wait before the call of the functions and call them when the time has been elapsed.
I only make some assumptions. So I would like to know exactly how javascript works and how setTimout/setInterval
works behind the scene.
Another question is: Are all asynchronous functions implemented based on setTimout
to create the asynchronous behaviour? I.e. the fact that the code is executed in a "kind of" parallel (code executed as soon as javascript has the time). For instance how fs.readFile('/etc/passwd', 'utf8', callback);
works?
var fs = require('fs');
fs.readFile('/etc/passwd', 'utf8', function(){
console.log("ICI")
});
var i = 0;
while(true)
{
++i;
if(i == 2000000000) break;
}
console.log("LA");
In that example we see that all the while loop is executed then "LA" is printed and AFTER "ICI" is printed. So the asynchronous call is executed when javascript doesn't have nothing to do like the previous case with setTimout
.