Checkpoint 1

Q1. In what order will the logs be printed?

setTimeout(() => {
  console.log(1);
  setTimeout(() => {
    console.log(2);
  }, 0);
}, 0);
queueMicrotask(() => {
  setTimeout(() => {
    console.log(3)
  }, 0);
  console.log(4);
});

Q2. Suppose the button is clicked, in what order will the logs be printed?

<div>
  <button>button</button>
</div>
const div = document.getElementsByTagName("div")[0], button = document.getElementsByTagName("button")[0];

button.onclick = () => {
  console.log(1);
  setTimeout(() => {
    console.log(2);
  }, 0)
}
div.onclick = () => {
  console.log(3);
  queueMicrotask(() => {
    console.log(4);
  });
}

Q3. In what order will the logs be printed?

const fn = () => (
  new Promise((res, rej) => {
    console.log(1);
    setTimeout(() => {
      console.log(2);
      res();
    }, 0)
  })
);

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

 

Last edited on 15/04/2024