Skip to content

Incorrect Promise.then() microtask execution order #44

@aaty2020

Description

@aaty2020

Hi and thanks for this helpful tool!

I noticed an issue with the event loop simulation where the order of Promise.then() callbacks seems to be incorrect.

Here's the code I tested:

`console.log('script start');

setTimeout(() => {
console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
console.log('promise 1');
}).then(() => {
console.log('promise 2');
});

console.log('script end');
`

In the simulator, the output is:

script start promise 2 script end promise 1 setTimeout

But in a real browser environment (tested in Chrome), the expected output is:

script start script end promise 1 promise 2 setTimeout

This happens because .then() chains are microtasks, and their callbacks are executed in order, immediately after the current script finishes and before the next macrotask.

It seems the simulator might be executing chained then()s in reverse or out of queue order.

Just wanted to report this in case it's helpful. Thanks again for the great tool

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions