I try to understand how JS processes asynchronous methods and finally I have come to async/await. Trying to get a full insight, I have created this 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
So, I have a question, why does it have such an order, and how JS's EventLoop works with async/await
I tried to create some other examples with similar syntax but the result is the same