Exploring the intricacies of how JavaScript processes asynchronous methods led me to dive into async/await. In an effort to gain a complete understanding, I crafted the following example:
async function first() {
console.log(9);
await Promise.resolve(2).then((r) => console.log(r));
console.log(0);
await Promise.resolve(3).then((r) => console.log(r));
}
async function second() {
console.log(10);
await Promise.resolve(4).then((r) => console.log(r));
console.log(11);
await Promise.resolve(5).then((r) => console.log(r));
}
first();
second();
const promise = Promise.resolve("new Promise");
promise.then((str) => console.log(str));
//The output:
//9
//10
//2
//4
//new Promise
//0
//11
//3
//5
Thus, it raises the question: what is the reasoning behind this sequence, and how does JavaScript's EventLoop interact with async/await?
I have attempted to replicate similar syntax in other examples, but the outcome remains consistent.