*******Please help me guys to clear this doubt Did any of you face this issue ******* How come readFile is executed before setImmediate even though the file is very large about 90mb? const fs=require('fs') //Top level code gets executed first in the main thread console.log("Program has started") //Timer callback stored in the first phase of event loop setTimeout(()=>{ console.log("Timer callback executed") },0) //After 1s pushed in the main thread if it is empty //If the timer has 1000ms as time then in the event just the timer starts and it moves to the next phase of the event loop and again when it comes to the same phase if timer has complete //IO third phase might take time callback function willbe pushed to the main thread only when io task is completed fs.readFile("../PipeMethod/large-file.txt","utf-8",()=>{ console.log("Async callback read file executed") }) //stored in the callback queue in the third phase of the event loop setImmediate(()=>{ console.log("setImmediate callback executed") }) //According to node js documentation setImmediate should expire before any other timer but that is not always the case. //Top level code gets executed first in the main thread console.log("Program has completed") /Output Program has started Program has completed Timer callback executed Async callback read file executed setImmediate callback executed How come readFile is executed before setImmediate even though the file is very large about 90mb?
I must say, this is one of the most exceptional videos I've come across online. Your dedication and effort are truly commendable, sir. Thank you for sharing your expertise with us!
thanks for the explanation sir but I have a question setTimeout(() => { setTimeout(() => { console.log('nested settimeout finishes'); }, 0); setImmediate(() => console.log('Immediate 1 finishes')); }, 0); why setImmediate() here gets called before the inner setTimeout() shouldn't the inner setTimeout gets executed before the setImmediate here because the event loop starts in the timer phase and executes the outer setTimeout then adds the inner setTimeout to the timer phase queue which the event loop is still currently at and then add the setImmediate callback to the third phase now the event loop is still at the timer phase why the inner setTimeout callback is not executed first before the setImmediate callback that is in the third phase
Hey, I think in the explanined example while dealing with file it is in 2nd phase(IO Tasks and Polling). Eventhough there are inner things going on, the next will be execute i.e, "Set immediate callback." So that's why set Immediate gets executed immediately. But if it contains any time consuming tasks then in the next control Timers and Set-immediate will be executed. I think....
as outer setTimeout() callback is executed it is in the first phase, callback have setTimeout() and and setImmediate which will run asynchornouly , as both expired immediately inner setTimeout()'s callback will store in first phase callback and setImmediate()'s callback will be stored in 3rd phase callback queue. Now as the callback of outer setTimeout get executed now event loop will move to the 2nd phase and then 3rd phase where it get callback function waiting for execution... and executed first even before the inner setTimeout() callback
As explained by a person above, as soon as the event loop pushes the callback to the main thread, it moves to the next phase as it doesn't know what are the things in the callback. That job is of the main thread to decide which code goes where. So event loop reaches the 2nd phase and setTimer has to wait
22:02 that means event loop phases can have nested event loop as well if our program needs if code have call backs in call backs. i think thats the reason call back hell is bad. am i right?
I have a doubt at 23:31. When the event loop is in 2nd phase and reaches setImmediate, instantly its callback will be pushed to the callback queue. At this loint event loop doesnt know whats ahead of it. It doesnt know process.nextTick is the next line. Why event loop doesnt execute 3rd phase callback even though event loop is in 3rd phase and the stack is empty ?
haven't seen someone teaching so clearly in my entire clg life
Thanks
I Have Never Find Someone Explaining The Node.js Like You , You Are Amazing For Sure..!
I second it
I third it
I 4th it
*******Please help me guys to clear this doubt Did any of you face this issue *******
How come readFile is executed before setImmediate even though the file is very large about 90mb?
const fs=require('fs')
//Top level code gets executed first in the main thread
console.log("Program has started")
//Timer callback stored in the first phase of event loop
setTimeout(()=>{
console.log("Timer callback executed")
},0)
//After 1s pushed in the main thread if it is empty
//If the timer has 1000ms as time then in the event just the timer starts and it moves to the next phase of the event loop and again when it comes to the same phase if timer has complete
//IO third phase might take time callback function willbe pushed to the main thread only when io task is completed
fs.readFile("../PipeMethod/large-file.txt","utf-8",()=>{
console.log("Async callback read file executed")
})
//stored in the callback queue in the third phase of the event loop
setImmediate(()=>{
console.log("setImmediate callback executed")
})
//According to node js documentation setImmediate should expire before any other timer but that is not always the case.
//Top level code gets executed first in the main thread
console.log("Program has completed")
/Output
Program has started
Program has completed
Timer callback executed
Async callback read file executed
setImmediate callback executed
How come readFile is executed before setImmediate even though the file is very large about 90mb?
This guy is amazing ! Never keeps a black hole In any of his videos !
wow ! what the lecture it is? the most beautiful thing is the educator does not have any hurry and he explain each and every point very calmly .
Your node.js series is awesome sir. The way u explain is the best among all other RUclips channels 👏🏻🙇🏻♂️
What a explain .................Superve
I must say, this is one of the most exceptional videos I've come across online. Your dedication and effort are truly commendable, sir. Thank you for sharing your expertise with us!
Wow such a nice explanation
Very informative..... got the event loop the way i want
Very clear Explanation, thank you.
Started binge watching this playlist. It's awesome.
Your articulation and presentation is too good.
these lectures are really helpful sir,thank you!!
Your series is outstanding. The way you're explaining how things works behind the scene is awesone 🥰 Love it 🥰
the best explanation ever!
You are champ❣️
Keep it up bro
Lots of love from delhi/ncr
thanks you sir. You teached very clearly and explain so well.
Very well explained, amazing job! Thank you!
great efforts
Thank you very much for the great explanation
So much clarity
Great !
amazing man!!
❤❤❤
amazing explanation I mean it !!
Guru ji, app insaan nhi, "Maha purush" h.....
thanks for the explanation sir but I have a question
setTimeout(() => {
setTimeout(() => {
console.log('nested settimeout finishes');
}, 0);
setImmediate(() => console.log('Immediate 1 finishes'));
}, 0);
why setImmediate() here gets called before the inner setTimeout()
shouldn't the inner setTimeout gets executed before the setImmediate here
because the event loop starts in the timer phase and executes the outer setTimeout then adds the inner setTimeout to the timer phase queue which the event loop is still currently at and then add the setImmediate callback to the third phase
now the event loop is still at the timer phase why the inner setTimeout callback is not executed first before the setImmediate callback that is in the third phase
I thought of the same. Let me know if you find the answer :)
Hey, I think in the explanined example while dealing with file it is in 2nd phase(IO Tasks and Polling). Eventhough there are inner things going on, the next will be execute i.e, "Set immediate callback." So that's why set Immediate gets executed immediately. But if it contains any time consuming tasks then in the next control Timers and Set-immediate will be executed. I think....
as outer setTimeout() callback is executed it is in the first phase, callback have setTimeout() and and setImmediate which will run asynchornouly , as both expired immediately inner setTimeout()'s callback will store in first phase callback and setImmediate()'s callback will be stored in 3rd phase callback queue.
Now as the callback of outer setTimeout get executed now event loop will move to the 2nd phase and then 3rd phase where it get callback function waiting for execution... and executed first even before the inner setTimeout() callback
As explained by a person above, as soon as the event loop pushes the callback to the main thread, it moves to the next phase as it doesn't know what are the things in the callback. That job is of the main thread to decide which code goes where. So event loop reaches the 2nd phase and setTimer has to wait
22:02 that means event loop phases can have nested event loop as well if our program needs if code have call backs in call backs. i think thats the reason call back hell is bad. am i right?
That's absolutely right
@@procademy thanks man 🫡
You should be a lecturer by default. I didn't watch it twice before understanding the concept.
I have a doubt at 23:31. When the event loop is in 2nd phase and reaches setImmediate, instantly its callback will be pushed to the callback queue. At this loint event loop doesnt know whats ahead of it. It doesnt know process.nextTick is the next line. Why event loop doesnt execute 3rd phase callback even though event loop is in 3rd phase and the stack is empty ?
cant understand your question