You have to understand stack and queue:
More information: stack and queue video or ducumentation
When you do a new Promise(fnResolvingOrRejecting)
the function fnResolvingOrRejecting
is immediately called so it's on the same stack. Nothing is queued yet.
If you understand what stack and queue is you could explain your code better omitting the confusing async and await syntax.
The syntax would really improve code once you understand what it actually does but in this case I'll leave it out so you can see what code is executed as a callback/result handler and what code is on stack.
function fn1 () {
console.log(1);//on the same stack as calling fn1
fn2()//executing fn2
.then(//first queued
() => console.log(3)
);
};
function fn2 () {
return new Promise(
(resolve)=>{
console.log(2);//on the same stack as calling fn1
resolve();
}
);
};
fn1();//calling fn1 immediatly logs 1 and 2 because they are on the same stack
new Promise((resolve, reject) => {
//after calling fn1 and queing console.log(3) this is executed because it's on
// the same stack
console.log(4)
resolve()
}).then(() => {//second queued
console.log(5)
})