In our last article we talked about call stacks, stack frames, stack overflow and a bunch of other JS stuff. We understood how the engine relates w...
For further actions, you may consider blocking this person and/or reporting abuse
First, thank you again for this series, Lucas :) I wanted to note something here. I see it already been discussed but I wanted to write it anyway.
I've changed the
setInterval
as it confused me a little bit and I wanted to differentiate each interval easily. The output in the post shows an extrasetInterval
right aftersetTimeout 2
, and it was really confusing to try to figure out where it came from. Once I ran the script on my computer, withNode 12.8.4
, I got the following output, which at this point things were a bit more clear. The interval actually runs twice. Compiling the code in my head whilst following the output helped a little.My guess is that it clears the interval before running it. Maybe the
clearInterval()
's behavior changed at some point? I'm not sure.thank you
Wow, this explanation is so great, thanks Lucas.
Thanks a lot man! I hope you enjoyed :)
Sure and I think Vert.x, which I'm using in my backend for a temporal, non-blocking REST-API is almost the same (I think they called it multi reactor pattern), but per default using 2 event loops per CPU.
log output of last example is something more complicate:
script start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setInterval
setTimeout 2
promise 5
promise 6
Thanks for the reply! I'll re-evaluate the example and see if something changed!
Hi, Lucas!
The output above is from Chrome browser, Ubuntu.
Here is from Node.js v10.17.0 Ubuntu:
script start
promise 1
promise 2
setInterval
setTimeout 1
promise 3
promise 4
setInterval
setTimeout 2
promise 5
promise 6
It seems environment affects how is queuing of macro tasks is.
Yes, the environment might change the order of how some of the tasks are processed. Node uses libuv whereas Chrome does not.
Hi lucas, your explanation was great and crisp and output is also right according to your explanation setInterval wont get called. Before that settimeout 2 's micro task promises will get printed and then set interval macro task should proceed as next tick but because interval got cleared it wont.
Sorry, I did not understand your question, could you please rephrase it?
Hi lucas, your explanation was great and crisp and output is also right according to your explanation setInterval wont get called. Before that settimeout 2 's micro task promises will get printed and then set interval macro task should proceed as next tick but because interval got cleared it wont.
Did you happen to copy or inspired from this source by Alexander Zlatkov at link.medium.com/bWcli9Hbu0?
If you copied it from here, it is not good to not mention where you took those pics and explanations.
I recently read an article about Event Loop that I felt very confused with a view contrary to you, the author of the article said that the event loop is the one that handles everything, not callstack (counting both synchronous code).Please let me know your views on this article.
nodejs.dev/the-nodejs-event-loop
Thanks for very detailed explanation! I'm liking this series a lot.
I just have a tiny question, on the fourth tick, setInterval handler is run which counts as a macrotask, and exactly one macrotask should be processed from the macrotask queue in a single tick. So wouldn't setTimeout 2 handler (which is another macrotask) be deferred to the fifth tick in the loop? I'm not sure.
Your explanation is awesome.
Thanks!!
It was very useful, thank you!
Very clear and nice explanation. Thanks .
Great post Lucas
Thank you for this amazing explanation, things are so clear now wow !