Skip to content

Event Loop implementation is wrong. It runs all macrotasks before running microtask #1150

@ocodista

Description

@ocodista

Describe the bug

If you run this code on Node.js

setTimeout(() => {
  console.log('First macrotask');
  queueMicrotask(() => console.log('Add microtask from macrotask'));
  queueMicrotask(() => console.log('Add microtask from macrotask'));
  queueMicrotask(() => console.log('Add microtask from macrotask'));
}, 0);
setTimeout(() => {
  console.log('Second macrotask');
}, 0);

The output is
image

This is because Node original Event Loop runs:

  • All microtasks
  • 1 Macrotask
  • All microtasks

Per loop.

And this is what happens when I run the same code on stackblitz:
image

It seems your Event Loop is running

  • All microtasks
  • All Macrotasks
  • All Microtasks

Per loop.

Link to the blitz that caused the error

https://stackblitz.com/edit/node-4egzkj?file=index.js

Steps to reproduce

  1. Run node index.js
  2. See the wrong output

Expected behavior

Stackblitz should run 1 macrotasks, then all microtasks before running the next macrotask

Parity with Local

Screenshots

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions