Question:
I am new in working with async keyword.
Below is the example make me confused. In my understanding, with async keyword, program would wait until the function after await.
So, I expected "Show at the end" would only show up at the very last.
Q: But why does "Show at the end" occur first? Is there a full explanation behind this?
Below is my present code:
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
const result = await resolveAfter2Seconds(); //The whole program would stop here until the resolveAfer2Seconds finish
console.log('calling');
console.log(result);
}
asyncCall();
console.log('Show at the end') //My expectation is this will only print at the end, as we got a await inisde asyncCall
Here is the result:
Show at the end
resolved
calling