Re:V8 adds support for top-level await
by laughinghan on Wednesday September 25, 2019 @04:43PM
(#21075201)
> maybe it only returns a promise on Tuesdays, or at random like some other commenter wrote
But async/await makes this better, because a function marked async always returns a promise.
Re:V8 adds support for top-level await
by minitech on Wednesday September 25, 2019 @03:49PM
(#21074572)
> But it can change under your feet – someone might change a function to be async, that used to be sync, and now your code is broken.
Someone might change a function that returns a single value to return an array, and now your code is broken. Someone might rename the function, and now your code is broken. This is the nature of breaking changes, and the same solutions apply.
Yeah but that changes the semantics of the function, whereas `async` arguably just changes the mechanics. The promise itself is not interesting, it's whatever value it (eventually) returns. My point is that a language that had implicit await would let you go on making function as async as you want them to be, and callers would be none the wiser. It'd also allow the caller to decide when to run things asynchronously and even truly defer values, something which JS promises can't do since they immediately execute, but that's an altogether different discussion.
It may be that the docs are bad and don't even tell you it returns a promise. Heck, maybe it only returns a promise on Tuesdays, or at random like some other commenter wrote – you'd have to then sprinke `await` there to make sure you're ok, even if most of the time you don't need it.