Tell me something about macro tasks and micro tasks. Many interviewers don’t understand them either. You can bluff him with one!

define

  • Macro tasks: common timers, user interaction events, etc. (macro tasks are specific to these tasks, no special meaning)
  • Microtasks: Promise related tasks, MutationObserver, etc. (again, just a name!!)

Er,

function app() {
  setTimeout(() = > {
    console.log("1-1");
    Promise.resolve().then(() = > {
      console.log("2-1");
    });
  });
  console.log("1-2");
  Promise.resolve().then(() = > {
    console.log("1-3");
    setTimeout(() = > {
      console.log("3-1");
    });
  });
}
app();
Copy the code

The output

1-2, 1-3, 1-1, 2-1, 3-1