Hi, process.nextTick callbacks are executed immediately. As mentioned, whenever the event loop encounters process.nextTick, it finishes its current callback execution(no matter what phase it is in), then pauses and executes our process.nextTick callback first after which it resumes to the phase which it left its work on.
Hey, pardon for the late reply. Yes, it happens in the execution context. Thanks for reminding, I think I forgot to mention this term in the article! By the way, I think you may have mistaken for the output, after executing, the output seems to be:
barfooimmediate
Here is how it happens:
console.log gets logged
f is called. f()
now, everything happens according to the f() context. i.e process.nextTick will execute with priority. If there are any other setTimeout, setImmediate callbacks, they will be pushed to the respective queues and executed.
lastly, when the function returns, we come back to the last setImmediate, and execute the callback.
I never tried my code. I thought process.nextTick will be push to event loop and setImmediate will run next since its in the higher or outer execution context.
Hello nice article. I have clarifying question
process.nextTick()
happens after all the event loop phases or before the phases?Hi,
process.nextTick
callbacks are executed immediately. As mentioned, whenever the event loop encountersprocess.nextTick
, it finishes its current callback execution(no matter what phase it is in), then pauses and executes ourprocess.nextTick
callback first after which it resumes to the phase which it left its work on.I hope you this answers your question.
Thank you for the response sir. Last question it only happens in their execution context right? say I have this code:
output:
bar
immediate
foo
even though I called f() first before setImmediate
Hey, pardon for the late reply. Yes, it happens in the execution context. Thanks for reminding, I think I forgot to mention this term in the article! By the way, I think you may have mistaken for the output, after executing, the output seems to be:
Here is how it happens:
I never tried my code. I thought process.nextTick will be push to event loop and setImmediate will run next since its in the higher or outer execution context.
anyway thanks for explaining. :)
Every code gets executed inside execution context only right? then why that question was raised? Am I not understanding something? pls help!