4 ms·
So, this makes me a little sad because the title makes it seem like top-level await is, in general, a bad thing, but upon closer reading, there only seems to be
by johnhenry 10y ago
So, this makes me a little sad because the title makes it seem like top-level await is, in general, a bad thing, but upon closer reading, there only seems to be an issue with using await in conjunction with import -- is this a correct assertion, or am I missing something? I've been making heavy use of await lately, and would love if it were available at the top level, but I have zero intention of using it in the way it's described in the article?
- rich_harris 10y agoWhether or not it's a module or something else being awaited isn't the problem. The issue that if your app.js (or whatever) has dependencies on modules with top-level awaits, however far removed, it can't execute until they're done. In other words top-level await essentially blocks your entire app. Using await inside an explicitly async function is great, because it only blocks code inside that function – the effects are localised and much easier to predict/reason about.
- johnhenry 10y agoAh, thank you, that makes sense. The term "footgun" is pretty interesting here, as it generally refers to something that allows one to shoot one's self in the foot. In this case, it's allows someone else to shoot you in the foot by hiding it in their code :/.
- rimunroe 10y agoOn the subject of whether or not this is really a problem because people can just be educated not to do this, the author said: > You'll educate some of them, but not all. If you give people tools like with, eval, and top-level await, they will be misused, with bad consequences for users of the web. This is still a footgun, because it makes it easy for people to introduce wide ranging and sometimes subtle bugs in their software.
- Bartweiss 10y agoAnd as we all know, what JS is missing is a new way for unknowing devs to build slow and buggy products.
- jbpetersen 10y agoNow we just need asynchronous imports and all is well again. I can't even tell whether I'm being sarcastic or not.
- jessaustin 10y agoPresumably you'll be able to tell at some point in future?
- Tloewald 10y agoNo, he provides a simple example of loading a page depending on a sequence of json operations -- code after the first await is stalled until they all finish. await import just makes problems worse and, probably, almost unavoidable.