Skip to content
Junior–Senior165 views

How will the output to the console occur when mixing setTimeout, Promise, and recursion

Task condition

It is necessary to determine the sequence of output to the console when the program includes synchronous instructions, microtasks (Promise), macro tasks (setTimeout), and also has a recursive function call.

console.log(1);

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

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

console.log(4);

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

console.log(6);

const foo1 = () => {
  console.log('foo1');
  return Promise.resolve().then(foo1);
};

foo1();
Frontend tasks in the code trainer