0

I am new to learn javascript; I encountered a big confusion when I was learning async await.

async function async1() {
  console.log("async1 start");
  await async2();
  console.log("async1 end");  // why will this task be placed in microtask?
}
async function async2() {
  console.log("async2");
}
async1();
console.log("script end");

I don't understand why will "console.log("async1 end")" be placed in microtask? in my opinion, the "console" should be a synchronized task rather than an asynchronous task.

2
  • 2
    async-await is syntactic sugar for Promise(fn).then(res, rej), the code part after await is just the same as res callback passed to then. Commented Mar 2, 2023 at 6:11
  • 2
    "in my opinion, the "console" should be a synchronized task rather than an asynchronous task." then it would run before the async2 finishes. Which doesn't make any sense considering you've said await for it. Also, were it true, consider that await would be completely useless construct to keep code in line let x = await p(); console.log(p) would never log anything useful then. Commented Mar 2, 2023 at 6:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.