Why is this an async function? It wants every function above it be async. Why not just make all functions async by default in JavaScript? And then get rid of the keywords. Wait…
@alex According to this article, WebCrypto heavily uses async operations because they can be slow (out of necessity), and they don't want to block rendering or input.
@RealAkoSuminoe Great find, but here’s what I don’t get: you can turn any sync function into an async function by calling setTimeout on it. However, it’s IMPOSSIBLE to turn an async function into a sync function. So why not make everything available as a sync function? Or even let us force a function to be sync? The bullshit StackOverflow answers say “because JS is an inherently asynchronous language”, but that doesn’t add up.
@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.