async await execution order demo
docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
by Vladislav Derbenev
HTML
<html>
<body>
<pre>'await' stops execution of the async function (step out)
'then' returns promise</pre>
</body>
</html>
JavaScript
(()=> {
const p = Promise.resolve();
const p2 = Promise.resolve();
(async() => {
p.then(() => console.log('in then')) // will do when resolved, 6
.then(() => console.log('2 in then')) // 7
.then(() => console.log('3 in then')) // 9
console.log('before await') // 1
await p; // 2, stop async function execution
console.log('after await') // 5
await p2;
console.log('after await 2') // 8
})()
.then( ()=>
console.log('after async') // 8
)
console.log('before then') // 3
console.log('after then') // 4, return execution to await p
})()