Skip to content
YouTurkey Tech
All JavaScript topics

Event Loop

Intermediate

Why setTimeout(…, 0) still runs after the rest of your code, and where promises fit in.Builds on Synchronous vs Asynchronous

Call stack

Empty

    Web APIs

    Empty

      Microtask queue

      Empty

        Task queue

        Empty

          Event loop
          1. Running code
          2. Running microtasks
          3. Taking the next task
          4. Waiting
          Console

          Empty

          0/17Press Play, or step through with the arrows, to begin.
          Step 0 of 17

          Explanation

          2 min read

          On this page

          What is the event loop in JavaScript?

          JavaScript runs on a single thread: one call stack, one thing at a time. Slow work such as timers, network requests and user events is handed to the environment (the browser's Web APIs, or Node.js). When that work finishes, its callback waits in a queue. The decides when each waiting callback gets to run.

          How does the event loop decide what runs next?

          Callbacks wait in one of two queues:

          • The microtask queue: promise callbacks (then, catch, finally) and the code after an await.
          • The task queue, also called the macrotask or callback queue: timers, events and I/O.

          The loop repeats one rule:

          1. Run the current task until the call stack is empty.
          2. Run every microtask, including the ones queued along the way.
          3. Take the next task from the task queue and start again.

          Microtasks always run before the next task. That is why a resolved promise beats setTimeout(…, 0).

          What mistakes do people make with the event loop?

          Mini example

          setTimeout(() => console.log('task'), 0);Promise.resolve().then(() => console.log('microtask'));console.log('sync'); // sync// microtask// task

          Quick check

          In which order does the mini example log its lines?

          Further reading

          Watch the video

          The full explanation from the YouTurkey Tech YouTube channel, in Arabic.

          All steps as text
          1. Step 1:

            The whole script runs as one task. It goes onto the call stack and runs synchronously, top to bottom.

          2. Step 2:

            console.log('start') runs immediately and prints start.

            Line 1

          3. Step 3:

            setTimeout is called, so it goes on top of the call stack.

            Lines 3–5

          4. Step 4:

            setTimeout hands the callback to the browser's timer, a Web API, and returns at once. The script does not wait.

            Lines 3–5

          5. Step 5:

            The 0 ms timer is done, so the callback moves to the task queue. It must wait there until the call stack is empty.

            Lines 3–5

          6. Step 6:

            Promise.resolve() gives an already resolved promise, and .then is called on it.

            Line 7

          7. Step 7:

            Because the promise is already resolved, .then queues its callback as a microtask right away.

            Lines 7–9

          8. Step 8:

            console.log('end') prints end. Both callbacks are still waiting.

            Line 11

          9. Step 9:

            The script task is done and the call stack is empty. Before anything else, the event loop empties the microtask queue.

          10. Step 10:

            The promise callback moves onto the call stack.

            Lines 7–9

          11. Step 11:

            It prints promise.

            Line 8

          12. Step 12:

            The callback returns. The microtask queue is now empty.

            Line 9

          13. Step 13:

            Only now does the event loop take the next task from the task queue.

          14. Step 14:

            The timeout callback finally moves onto the call stack.

            Lines 3–5

          15. Step 15:

            It prints timeout.

            Line 4

          16. Step 16:

            The callback returns. With the stack and both queues empty, the event loop waits for new work.

            Line 5

          17. Step 17:

            The output is start, end, promise, timeout: synchronous code first, then microtasks, then tasks.