Take a look at the following code:
function promised() {
return new Promise((resolve) => {
setTimeout(() => resolve('Timeout'), 1000);
});
}
async function awaited()
{
const r = await promised();
console.log('Line 10: ' + r);
return r;
}
function start() {
const r = awaited();
console.log('Line 16: ' + r);
}
start();
console.log("All done");
When you run this code, you'll get:
Line 16: [object Promise]
All done
Line 10: Timeout
But I was expecting to get:
Line 10: Timeout
Line 16: Timeout
All done
My expectation was that await should have blocked the execution on line 9. But it seems it does not! My question is why?
awaitedexecution pauses,start's does not as it is not an async method with anawaitstatement. Async methods don't ever block, that is their main purpose to not block. Synchronous functions block, hence why it is advised not to use methods like sync ajax requests.