Sobes.tech
Junior — Senior

Determining the output sequence of console.log with a mixed microtask and macrotask queue

livecode

Task condition

It is necessary to determine the order in which messages will be output to the console, considering execution priorities: first synchronous code, then microtasks (Promise), and after them macrotasks (setTimeout).

console.log(1);

setTimeout(() => console.log(2));

Promise.resolve().then(() => console.log(3));

Promise.resolve().then(() => setTimeout(() => console.log(4)));

Promise.resolve().then(() => console.log(5));

setTimeout(() => console.log(6));

console.log(7);