Embed Notice
HTML Code
Corresponding Notice
- Embed this notice@alex I generally don't even look at SO because its full of bad takes.
Making it possible to do turn an async operation into a sync operation would become a really easy way for developers to do the wrong thing, and potentially block the main thread with a long running operation. The web specs are designed defensively for an environment where there are lots of 3rd party scripts on most pages, each with the ability to theoretically break things. It may suck for some things, but giving developers an easy way to do the wrong thing guarantees that people will do the wrong thing. We want more things to be async, not less.
We can't just make every function asynchronous and get rid of async/await all together because we still need to have a Promise primitive to get any benefits from it. You couldn't do something like this if everything were automatically await'ed:
var taskA = heavyTask1(); // starts A
var taskB = heavyTask2(); // starts B in parallel with A
var resultA = await taskA;
var resultB = await taskB;
// OR
var [resultA, resultB] = await Promise.all([taskA, taskB])
If taskA encounters a network request, it can run taskB while it waits for the result and then switch back to taskA.