I'm extremely confused why the following code returns what it does when I write it using both promise chaining and async/await syntax:
function log() {
console.log("Hello from inside second await")
}
async function test() {
await console.log("Hello from first await");
await setTimeout(log, 1000);
await console.log("Hello from third await");
}
test();
console.log('Where does this occur?')
I thought the await keyword made the code asynchronous. Why is the first await printing to the console instantly ahead of the console.log in the main thread? Additionally, why is the second await occurring after the third? Isn't the purpose of await to make sure that important data for future functions exists before they're executed? This makes no sense to me, help would be greatly appreciated.
The async function means it will be to executed somtime. More from javascript async functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function