JavaScript Event Loop: How it Works and Why it Matters in 5 Minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ย. 2024

ความคิดเห็น • 181

  • @notarealhandle123
    @notarealhandle123 8 หลายเดือนก่อน +7

    I've been developing in JavaScript for over 20 years now. But when I was interviewed recently for a new job, and asked about JavaScript event loop, I could not remember it for the life of me. This is mainly academical knowledge, not something that you need as an experienced developer. People who do interview are often dumb, and do not understand that.

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj หลายเดือนก่อน

      ...this video explains it poorly actually.
      and you do need to know it.. you're wrong. not knowing event loop and execution context and enviorement and the phases.. is how people fck themsealves with accidental closures and other stuff...
      most important part to know about event loop is about task queue and microtask queue, how microtask queue has priority,
      and that web apis.. some of them are callback based, some of them are promise based ..
      even setTimeout 1000ms for example.. is not 100% guaranteed,that it actually is 1000ms , that's not execution to time.. that's execution to enqueue of microtask queue.

  • @thanhattran284
    @thanhattran284 ปีที่แล้ว +44

    Finally, after looking for n^1000 videos, forums... I found a person who can explain these terms in real simple and understandable way 😭really appreciate your work man.

    • @rafadydkiemmacha7543
      @rafadydkiemmacha7543 5 หลายเดือนก่อน

      Really? It was a very lame explanation. He skipped many essential parts while dragging on single things.

  • @rick.d
    @rick.d 2 ปีที่แล้ว +35

    Understanding the LIFO/FIFO nature of the call stack vs the event stack is really cool, thank you.

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว

      So glad that was helpful :)

    • @goyii
      @goyii 8 หลายเดือนก่อน +3

      This is true but i think its important to not call event queue "stack" when the real data structure its a Queue. When you think about stacks and queues from real life everything makes sense

    • @Shagidelic
      @Shagidelic หลายเดือนก่อน

      you mean event queue, not stack. Call Stack -- is stack, event queue - is queue.

  • @Mo-Sam
    @Mo-Sam 2 ปีที่แล้ว +24

    I loved it, thank you. It would be so great if you make a series like this , explaining advanced concepts under 5 minutes.

  • @ming3957
    @ming3957 2 ปีที่แล้ว +31

    Expected a 5 minute video but I got 2 whole extra minutes of James for free! What a deal

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว +4

      Always here to please!

  • @GetGoodatAnything
    @GetGoodatAnything 4 หลายเดือนก่อน

    As someone who is new to coding that explanation made the overall process really clarified things for me. Cheers

  • @indraappkey
    @indraappkey ปีที่แล้ว +6

    one of the clearest explanation of event loop, thanks James

  • @georgenonis5967
    @georgenonis5967 10 หลายเดือนก่อน +1

    Amazing video. Whenever I forget how it works I just jump over here and everything is clear again. Thank you mate!

  • @marcelukeje3510
    @marcelukeje3510 7 หลายเดือนก่อน

    I'm here with no background knowledge on JavaScript. I need to know this for an assignment and I find the video insanely easy to understand

  • @denniszenanywhere
    @denniszenanywhere 2 ปีที่แล้ว

    I like this overexplanation. Please do more. I am even watching tiktok videos that loop short coding lessons and they’re sinking in more.

  • @shuvra03
    @shuvra03 ปีที่แล้ว

    Best video on node js event loop in 5 minutes

  • @misterl8129
    @misterl8129 8 หลายเดือนก่อน

    Thanks for the video! And yes, I have a question.
    So we know that every async procedure will go to the event queue. And it will only passed to the call stack if this is empty.
    So...
    1. What about if we have a large large project, with many sync procedures, the event loop will wait until ALL of them finish before pass it to the call stack and give the result?
    2. How it work then the "await", if we know that we need to clear the call stack before pass the async functionality. How it work when we need to have some data before proceed with the other instructions?
    For example
    data mutation 1,
    data mutation 2,
    data mutation 3,
    await for something()
    data mutation 4
    How it works when we need to have the "something" before go to the data mutation 4
    Thank you!

  • @eduardocuadraneyra
    @eduardocuadraneyra 8 หลายเดือนก่อน

    THNKAS A LOT i was havnig a hard time to get this idea but you have explained really simple, so Here you have a new student, thanks again and have a great day...and if your a reading this and your not the teacher , have a great day to smile, and remember you are important to.

  • @TerabyteTy300
    @TerabyteTy300 3 หลายเดือนก่อน

    Recently came across your channel and I’ve been learning so much. Thank you for all the work you put in on these!

  • @erishiraj
    @erishiraj 2 หลายเดือนก่อน

    Brilliant explanation with a clear design flow. Thanks, James!

  • @ahlembs790
    @ahlembs790 11 หลายเดือนก่อน +1

    i am really thankful for this video just before my interview, , feel having clear idea about what happen behind the scenes with node.js, please more advanced topics like this with such great explanantion

    • @JamesQQuick
      @JamesQQuick  11 หลายเดือนก่อน

      Wow, that's so cool to hear. Hope it went well!

  • @ghaithjr.9189
    @ghaithjr.9189 ปีที่แล้ว

    note : libuv is written primarily in c ,
    great explanation king

  • @Kanal5909
    @Kanal5909 ปีที่แล้ว +3

    For me the most tricky part is that you never really know when the function you call is going to be asynchronous or synchronous. For example if you read data from a file and then process it, for small files it will be synchronous and for the files with let's say 300k lines it will most likely be asynchronous (it also depends on what you do with these lines). It's obvious that all HTTP requests are asynchronous so this is quite easy to handle or predictable at least.

    • @JamesQQuick
      @JamesQQuick  ปีที่แล้ว +6

      Well, I think you can tell something is asynchronous based on two things:
      1. it returns a promise
      or
      2. if takes a callback function as a parameter

  • @Karan60191
    @Karan60191 4 หลายเดือนก่อน

    Hi , i was trying to wrap my head around the event loop, one suggestion for the video , a brief about v8 + libUv must have been wonderful specially mentioning about the 4 extra threads that libUv brings in so that the fundamentals becomes much more clear, anyways great video as ever :)

  • @umangkathuria7097
    @umangkathuria7097 8 หลายเดือนก่อน

    Always come here for a quick refresher before I have to explain it to anyone :) That article is excellent in itself too! great content!

    • @JamesQQuick
      @JamesQQuick  8 หลายเดือนก่อน

      Thanks for watching!

  • @igoeast
    @igoeast 5 หลายเดือนก่อน

    This is the best video that describes Event loop. Thank you so much for your work James!

    • @JamesQQuick
      @JamesQQuick  5 หลายเดือนก่อน

      Wow, thank you!!

  • @praveenvarma1
    @praveenvarma1 ปีที่แล้ว +1

    Super bro ... u explained a complex theory so simply with a beautiful animation.

  • @NikhilDahriya-o5y
    @NikhilDahriya-o5y 4 หลายเดือนก่อน

    Awesome. It was Crisp, Precise, Easy to understand and To the Point.

  • @anthonysim563
    @anthonysim563 2 หลายเดือนก่อน

    Omg, thank you for this explanation. This was so straightforward and easy to understand.

  • @SammedP-r9s
    @SammedP-r9s 6 หลายเดือนก่อน

    Really nice and simple way of explaining complex concepts. Great video

  • @johnshepard3197
    @johnshepard3197 ปีที่แล้ว

    Thanks for the quick refresher, man! Plus correcting my pronunciation of "lib-you-vee" library ;)

  • @manerVlog
    @manerVlog 8 หลายเดือนก่อน

    Awsome sir now I understand watching many videos this is very simple to understand.

  • @diegogutierrez7032
    @diegogutierrez7032 ปีที่แล้ว

    Something to add there is that. This is the event loop explained from the NodeJS side. If you take a look at the event loop in the browser side. It's much mure complex due to UI taks, Render Tasks, Dom Tasks, Net tasks.

    • @JamesQQuick
      @JamesQQuick  ปีที่แล้ว +2

      I’m not sure that’s true? How is it different? It still uses the same offloading mechanism for async work right?

  • @muhammad_faraz_ali
    @muhammad_faraz_ali 4 หลายเดือนก่อน

    Great content games🎉. Can you please share which software you used for visualisation?

  • @noorulamin2652
    @noorulamin2652 2 ปีที่แล้ว +1

    Awesome! I have few questions 1. few days ago i was reading a medium article in which they use the term callback queue but here you mentioned event queue. 2 they called it Node APIs and you mentioned Libuv APIs. 3. They also did mention what's called MicroTask Queue, but here it was missing. 4. Does Event Loop handles only Async operations? 5. How does Event Loop handles setTimeout functions?

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว +11

      So I believe "callback queue" would be the same as the "event queue". I would assume Node APIs are also referring to the Libuv APis as well. I have never heard of the MicroTask Queue, so I'm not sure where that fits in. For the event loop, yes, it is only looking for async operations that have finished running.
      setTimeout takes a callback function. So, when setTimeout is called, it sends it off to the underlying APIs to be handled. When the timeout is done, it then throws the event/callback onto the Event Queue. The Event Loop will find the event on the queue and pass it back to JavaScript call stack.
      hope that helps!

    • @ivansandoval3757
      @ivansandoval3757 ปีที่แล้ว +1

      Same doubt as yours, I just watched a different video where the event loop components are mentioned as follows: Call stack, event loop, micro tasks queue, macro tasks queue and scheduled tasks queue.

    • @And1997Ruz
      @And1997Ruz ปีที่แล้ว +2

      @@ivansandoval3757 You have probably watched the explanation of the browser event loop, whereas here it's the nodejs event loop
      They work in a different way. Not to mention the fact that browsers don't have the Libuv API

  • @SonAyoD
    @SonAyoD 2 ปีที่แล้ว

    Wow, I finally get it. Where does the heap come into this?

  • @felixurrutia4246
    @felixurrutia4246 ปีที่แล้ว +1

    Excellent explanation. This is exactly what I was looking for.

  • @DavyJones124
    @DavyJones124 ปีที่แล้ว

    this explanation was amazing

  • @alexeecs
    @alexeecs 8 หลายเดือนก่อน

    Great video. One complaint, this is the node event loop which isn't exactly the same thing as JavaScript

  • @rayenselmen603
    @rayenselmen603 ปีที่แล้ว

    Finally ! Amazing explanation Sir !

  • @DavidLevy-qg7hd
    @DavidLevy-qg7hd 6 หลายเดือนก่อน +2

    nice video but the title is a bit misleading, this is actually the node.js event loop as you say at the end of the video, not the javascript event loop as the title says.. JS natively does not use libuv, instead it uses the browser's built-in Web APIs to handle asynchronous tasks

    • @xuwu7299
      @xuwu7299 3 หลายเดือนก่อน

      Good correction

  • @dpetrik99
    @dpetrik99 ปีที่แล้ว

    Great and short tutorial! Highly recommended

  • @smachohalla
    @smachohalla ปีที่แล้ว

    Thanks Mr. Quick. This was neat magnificently explained

  • @khlgaming36
    @khlgaming36 2 หลายเดือนก่อน

    Broo you nailed it thank you and thanks to andrew for this informative explanation

  • @kasiad7291
    @kasiad7291 ปีที่แล้ว +1

    Perfect explanation, thank you!

  • @Yunraii
    @Yunraii ปีที่แล้ว

    Thank you very much. It was explained very well and I finally understood what an JS Even Loop is. You did a great job in explaining it!

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 ปีที่แล้ว

    Thank you! Very concise and simple explanation of not an easy concept! 💛

  • @Walocial
    @Walocial 10 หลายเดือนก่อน +1

    Awesome explanation, much appreciated. Thanks :)

  • @abdelrahman1669
    @abdelrahman1669 ปีที่แล้ว +1

    really impressive thanks man

  • @godswillumukoro8908
    @godswillumukoro8908 ปีที่แล้ว

    Great explanation here James!!!

  • @damianodamian7907
    @damianodamian7907 ปีที่แล้ว

    Now I got it, thank you. That is a really great explanation James! You've got a new sub

    • @JamesQQuick
      @JamesQQuick  ปีที่แล้ว +1

      Yayyyyy!!! Glad it helped!

  • @youngshehata2348
    @youngshehata2348 6 หลายเดือนก่อน

    That was amazing explaining, thank you so much

  • @maxlev621
    @maxlev621 ปีที่แล้ว

    Best explanation ever

  • @DO6INTech
    @DO6INTech 2 ปีที่แล้ว

    thank you pls do not stop thank you

  • @koffworld7857
    @koffworld7857 ปีที่แล้ว

    Very clear explanation... great video!

  • @galnadjar
    @galnadjar 3 วันที่ผ่านมา

    great explanation!

  • @afridperingaden846
    @afridperingaden846 ปีที่แล้ว

    very good explanation

  • @LucasAlves-si3jb
    @LucasAlves-si3jb 7 หลายเดือนก่อน

    Amazing explanation. Thank you!

  • @utkarshsinghalus3085
    @utkarshsinghalus3085 ปีที่แล้ว

    wow great video , it simple thanks for the virtual presentation

  • @ЂорђеЈеленковић
    @ЂорђеЈеленковић 2 ปีที่แล้ว

    Great video, keep going James! :)

  • @mstersierra-xyz
    @mstersierra-xyz 3 หลายเดือนก่อน

    thank you for explaining this!

  • @EmmanuelIbikunle
    @EmmanuelIbikunle ปีที่แล้ว

    Very good explanation. Thanks

    • @JamesQQuick
      @JamesQQuick  ปีที่แล้ว

      So glad it was helpful!

  • @jean-pierrebanchereau8474
    @jean-pierrebanchereau8474 ปีที่แล้ว

    Perfectly explained. Thank you

  • @Rashomon69
    @Rashomon69 ปีที่แล้ว

    Brilliant. I think I finally understand.
    SUBBED!

  • @tinaxing1020
    @tinaxing1020 ปีที่แล้ว

    This is amazing explanation !!! thank you!

  • @mohmmadjesus6710
    @mohmmadjesus6710 2 ปีที่แล้ว

    Wonderful Info Sir! please make a series on vue js

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว +1

      Glad you enjoyed it! I still haven’t really spent time with vue yet but I want to!

  • @user-wr4yl7tx3w
    @user-wr4yl7tx3w ปีที่แล้ว

    really well explained.

  • @asrnyagmur9802
    @asrnyagmur9802 ปีที่แล้ว

    Clearly understand ❤ followed

  • @Yan-vi6fd
    @Yan-vi6fd ปีที่แล้ว

    Amazing explanation!!

    • @JamesQQuick
      @JamesQQuick  ปีที่แล้ว

      So glad you found it helpful!

  • @ricardocnn
    @ricardocnn 2 ปีที่แล้ว +1

    Awesome tips!

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว

      Thank you! Glad it was helpful!

  • @Shagidelic
    @Shagidelic หลายเดือนก่อน

    now i can say for sure that asynchronous javascript is certainly not single threaded, because LibUV API is running a separate thread in parallel to javascript main thread.

  • @hendodesktasks
    @hendodesktasks ปีที่แล้ว

    Great Explanation, Thanks a lot

  • @elielberra2867
    @elielberra2867 ปีที่แล้ว

    Amazing explanation, it was very clear! Thanks :)

  • @alexmiserandino1062
    @alexmiserandino1062 10 หลายเดือนก่อน

    fantastic video, thanks!

  • @naveenAspiresys
    @naveenAspiresys ปีที่แล้ว +1

    For 5 mins it is fine, but the micro task loop concept is missing

  • @moneyfr
    @moneyfr 2 ปีที่แล้ว +1

    Getter and setter please and why using it

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว

      You're interested in why people use getters and setters?

  • @Pareshbpatel
    @Pareshbpatel 7 หลายเดือนก่อน

    A nice walkthrough the JS Event Loop. Thanks, James
    {2024-04-15}

  • @andrewwall2730
    @andrewwall2730 6 หลายเดือนก่อน

    Just a minor correction, libuv is written in C, not C++

    • @xuwu7299
      @xuwu7299 3 หลายเดือนก่อน +1

      Yes exactly

  • @reeq5748
    @reeq5748 ปีที่แล้ว

    U saved me man. Thank you.

  • @kacygilbert9434
    @kacygilbert9434 ปีที่แล้ว

    Thanks for this!!!

  • @dorels6205
    @dorels6205 ปีที่แล้ว

    Wow, really, really understandable!!!

  • @RihaLV
    @RihaLV ปีที่แล้ว

    Great video, thank you!

  • @hassanad94
    @hassanad94 10 หลายเดือนก่อน

    What about js workers? You can do multi threaded tasks with it.

  • @ghostkee5031
    @ghostkee5031 ปีที่แล้ว

    you gonna confuse people between the built in javascript event loop and nodejs thread pool + event loop which libuv provide

  • @TheZeiZei
    @TheZeiZei 2 ปีที่แล้ว

    Great explanation!

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว

      Thank you. Hope it helped!

  • @ctchen1988
    @ctchen1988 8 หลายเดือนก่อน

    awesome...thank you so much.

  • @nikman5541
    @nikman5541 7 หลายเดือนก่อน

    Okay this is Node platform with C++ wrapper. but what about browser where we dont have Node and still have async JS?

  • @ehtisham_hashmi
    @ehtisham_hashmi ปีที่แล้ว

    where I can find more videos on js concepts?

  • @liamdev5145
    @liamdev5145 ปีที่แล้ว

    Woa truly appreciate it

  • @mikkun_
    @mikkun_ 11 หลายเดือนก่อน

    so if we have async/await, it will put the asynchronous to libuv, then event queue, and stack it to the call stack along with the other synchronous tasks?

  • @krnkoravi
    @krnkoravi ปีที่แล้ว

    First time hearing about LIBUV API. I thought async task directly goes from callstack to event queue. LIBUV only applicable in nodejs? Still not clear what exactly processing LIBUV does to async task before it goes to event queue

  • @devonchia5976
    @devonchia5976 ปีที่แล้ว +1

    so if there is an incoming synchronous code and a callback at the same given time, what is the precedence for the call stack?

    • @AahadCyborg
      @AahadCyborg ปีที่แล้ว

      when synchronous code and a callback are both triggered at the same time, the synchronous code takes precedence over the callback function in the call stack. This means that the synchronous code will be executed first, and then the callback function will be added to the call stack and executed once the synchronous code has finished running.

  • @user-rdr1712
    @user-rdr1712 10 หลายเดือนก่อน

    Hi, 5:15 Eventloop use to take the event from queue and pushes into the callstack only when the callstack is empty. Means, eventloop do not take the event from the queue when there is an event(atleast 1) in the callstack. Am I right?
    What if there are multiple events waiting in event queue. Do the eventloop dequeues all the events one by one from eventqueue when the call stack is empty or it will dequeue only one event?

  • @TheBlackmanIsGod
    @TheBlackmanIsGod ปีที่แล้ว

    Explain the “popping off the top” more

  • @LeLeTesoro
    @LeLeTesoro ปีที่แล้ว

    Hi there, I'm watching this in April - if I sign up on the website you linked, can I still see the 24 JS and CSS challenges you did for Advent?

  • @scottweedo6707
    @scottweedo6707 ปีที่แล้ว

    Thanks 🙌🏼

  • @anushree3744
    @anushree3744 10 หลายเดือนก่อน

    what does it mean when we say that event loop is blocked and how does that happen?

  • @ofeyofey
    @ofeyofey ปีที่แล้ว

    He says the event loop is "first in last out" is that out from the place it came it in or other end?

  • @diwakardayal954
    @diwakardayal954 6 หลายเดือนก่อน

    Cleannnn thanks

  • @juliorodriguez4946
    @juliorodriguez4946 ปีที่แล้ว

    Thank you so much...

  • @neeravvinod2774
    @neeravvinod2774 10 หลายเดือนก่อน

    great video

  • @satorugojo6
    @satorugojo6 ปีที่แล้ว

    thanks a lot man

  • @iiinicky6224
    @iiinicky6224 ปีที่แล้ว

    Thanks!

  • @suryadevarakarthik1814
    @suryadevarakarthik1814 4 หลายเดือนก่อน

    how does nodejs handle this code
    async function doSomething() {}
    console.log('Hi')
    await doSomething()
    console.log('Bye')
    doSomething is pushed to the libuv?

  • @krateskim4169
    @krateskim4169 2 ปีที่แล้ว

    great explanation

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว

      Thank you! Did you already have a good idea of what the event loop is?

    • @krateskim4169
      @krateskim4169 2 ปีที่แล้ว

      @@JamesQQuick nope not until today, learnt something new

    • @JamesQQuick
      @JamesQQuick  2 ปีที่แล้ว +1

      @@krateskim4169 Ah so glad it helped!!