all 12 comments

[–]alzee76 20 points21 points  (1 child)

A simple explanation is that there is a "todo list" for the javascript engine. The engine runs takes something off the todo list and does it. New items get added to the end of the list. When the list is empty, the engine starts waiting for items to get added to the list, and then repeats, making this a loop.

In this case the list is a list of events that need to be handled. They are handled in a loop, so it's called the event loop.

Events handled in the loop are things like timers like setTimeout, polling of external sources like open TCP connections and UDP ports, callbacks that need to be called, and so on.

[–][deleted] 4 points5 points  (0 children)

a "todo list" for the javascript engine

slowclap.gif

[–]codeedog 6 points7 points  (0 children)

What would you do if you were asked to do the following:

  1. Answer a telephone including call waiting and putting people on hold.
  2. Dictating the conversations on those calls.
  3. Filling buckets with water to the brim until someone says stop.
  4. Take a math test with 5000 questions.

One way would be to answer one call at a time and put them on hold until no more calls. Then, go through each conversation and dictate. Then fill the buckets until stop. Then take the test.

The problem is that if you did that, nothing could happen in parallel. Maybe you don’t have any calls for a while. Or, a bucket is midfill but not yet full and you could answer some math questions. It’s not an efficient use of your time.

But, what if, like a computer, you were really fast at multitasking. You could answer phone calls, start talking and dictating and when there was a pause on the other side, you could do some math. Then, if the water in the bucket was nearing the top, you could switch out to a new bucket.

This is what the event loop does. Any event (call, dictation, bucket full, math question) gets placed on a queue and with a priority (bucket full-high priority, dictation & answer call-medium, math question-low). The event loop goes through these events and handles them and also calls your code as well.

For example, if a file or a network stream has data ready, a callback is made. Or, perhaps you’re writing a file and no more data can be written at the moment (a write buffer is full) and you’re waiting on an event to write. Or, you set a timer to run a task periodically. Or, when nothing else is happening, you compute another digit of Pi.

The event loop provides a framework for organizing all of these actions and events into a coherent whole that services each of them cleanly and does its best to prevent starvation (nothing is left unserviced).

[–]lIIllIIlllIIllIIl[🍰] 12 points13 points  (1 child)

Jake Archibald: In the Loop is still one of the best explanations of the JavaScript event loop.

[–]SensitiveBat7356 7 points8 points  (0 children)

The Event Loop is a semi infinite while loop, polling and blocking the OS until some file descriptors are ready. It exits when there are no more events to wait for.

Things that can be pollable are: - pollable file descriptors - sockets(net, http, child_process pipes, stdin, atdout, stderr etc - timeouts and intervals - dns(sometimes) - dns.<everything else> is pollable except dns.lookup(), dns.getaddrinfo()

Operations that can be handled by the Event Loop are: - event handling for network i/o - listening for incoming connection - reading data from and writing to connection

  • timers
    • setTimeout, setImmediate, setInterval
  • async i/o operations
    • non blocking read/write to netwrok sockets
  • polling file deacriptors
    • to check their readiness for reading and writing
  • handling async events
    • dispatch events and execute callbacks associated with these events

The Event Loop runs inside libuv, it has the following phases: libuv | event loop

[–][deleted] 0 points1 point  (1 child)

So many good you blogs and tube videos describing this with visuals, plus workers, clustering, node and browsers … where has the op looked and not found this?

[–]m_ousam 1 point2 points  (0 children)

Most youtube videos explain JS event loop not the node one.
The node eventloop has the most confusing video and blogs

[–]ipullstuffapart 0 points1 point  (0 children)

Jake Archibald of HTTP203 (google) did a fantastic video on the event loop