I have written the program below in efforts to understand the event-loop and functions like setTimeout and setInterval.
The output of the program is different from What I expected:
The output is:
In F
In L
Padalia
outside all
callback1
callback2
From Interval:0
From Interval:1
From Interval:2
From Interval:3
QUESTIONS:
PROGRAM:
var Fname = undefined;
var Lname = undefined;
var count = 0;
function F(callback){
console.log("In F");
Fname = "Rushabh";
if(Fname != undefined && Lname != undefined) {
console.log(Fname);
}
process.nextTick(function() {
callback();
});
//callback();
}
function L(callback){
console.log("In L");
Lname = "Padalia";
if(Fname != undefined && Lname != undefined) {
console.log(Lname);
}
process.nextTick(function() {callback();});
//callback();
}
function pute(){
Id = setInterval(function() {
console.log("From Interval:" + count); count++;
if(count > 3){
clearInterval(Id);
}
}, 100)
setTimeout(F(function(){
console.log("callback1");
}),5000);
setTimeout(L(function(){
console.log("callback2");
}) , 5000);
console.log("Outside all");
}
pute();
I have written the program below in efforts to understand the event-loop and functions like setTimeout and setInterval.
The output of the program is different from What I expected:
The output is:
In F
In L
Padalia
outside all
callback1
callback2
From Interval:0
From Interval:1
From Interval:2
From Interval:3
QUESTIONS:
PROGRAM:
var Fname = undefined;
var Lname = undefined;
var count = 0;
function F(callback){
console.log("In F");
Fname = "Rushabh";
if(Fname != undefined && Lname != undefined) {
console.log(Fname);
}
process.nextTick(function() {
callback();
});
//callback();
}
function L(callback){
console.log("In L");
Lname = "Padalia";
if(Fname != undefined && Lname != undefined) {
console.log(Lname);
}
process.nextTick(function() {callback();});
//callback();
}
function pute(){
Id = setInterval(function() {
console.log("From Interval:" + count); count++;
if(count > 3){
clearInterval(Id);
}
}, 100)
setTimeout(F(function(){
console.log("callback1");
}),5000);
setTimeout(L(function(){
console.log("callback2");
}) , 5000);
console.log("Outside all");
}
pute();
You have a bug in the code where you set F
and L
timeouts. Your code is equivalent to this:
/* ... */
F(function(){
console.log("callback1");
});
setTimeout(undefined ,5000);
L(function(){
console.log("callback2");
});
setTimeout(undefined, 5000);
/* ... */
Now it should be clear why your program does not behave as you were expecting:
F
and L
before.undefined
callback.The easiest way how to fix your code is to add anonymous callback function for setTimeout
calls:
setTimeout(function() { F(function(){
console.log("callback1");
})},5000);
setTimeout(function() { L(function(){
console.log("callback2");
})} , 5000);
Alternatively, you can use bind
to fixate F
and L
parameters (the first parameter of bind
is value for this
):
setTimeout(F.bind(null, (function(){
console.log("callback1");
})),5000);
setTimeout(L.bind(null, (function(){
console.log("callback2");
})) , 5000);
You can also change your setTimeout as follows,
...
setTimeout(F,5000,function(){
console.log("callback1");
});
setTimeout(L,5000,function(){
console.log("callback2");
});
...
As setTimeout function won't take the parameter directly to your function, you need to send them in subsequent parameters.
setTimeout(function,milliseconds,param1,param2,...)