8 ms·
Beware of Async/Await
- gamell 6y agoI wouldn't call it necessarily a "mistake" but certainly something you would want to avoid doing if you want your code to be performant.
- fastball 6y agoI actually like making this kinda mistake, because it means I can get some easy performance gains in a bugfix release!
- shauns 6y agoI think people miss the wait part of await and hence forget they can separate the creation and consumption of a promise. Should have been called waitFor!
- happytoexplain 6y agoAs a non-JS person, I thought it was obvious what await does, but now I'm second guessing myself. What does it do that's not the "wait" part? Is it not simply causing a normally asynchronous function to block? Edit: Sorry for the late edit, but: Is it because users are using it thinking it just "unwraps" a promise into its resulting value? I can begin to understand that, but it seems so incidental to the primary concept of "waiting" for something asynchronous to return.
- jakear 6y agoCurious - did anyone look at the first example and not immediately see that the requests would occur in series? IMO the “await” actually makes this much more explicit.
- zwieback 6y agoYeah, I couldn't understand what the article was about until the end because it's so obvious that the requests would not be in parallel.
- oauea 6y agoIt reads like it might be this guy's first week, this is extremely obvious. Confused why this has so many upvotes.
- pixelbath 6y agoYes - I thought that perhaps "await" did something different in JS than C#, but that is literally its entire purpose: to synchronously wait for a call within an asynchronous method. Even Wikipedia says the async/await pattern "allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function." Honestly not sure how this made it to even 20 points.
- crazygringo 6y agoCompletely. I had no idea what the "problem" was because it was so obvious that the intention of the code was to have them run sequentially. Next up: exception handling doesn't work if you never catch exceptions! Followed by, assigning a pointer to another variable doesn't assign the value the pointer points to! :P
- norswap 6y agoExactly, it's called AWAIT damn it. It's also behaves like synchronous I/O, which is the whole point of the notation. Which isn't to say all is rosy in async/await world. Debugging them (in JS in particular) is particularly painful.
- throwaway43234 6y agoWhat do you find painful about it? Have you tried the new vscode javascript debugger? It makes things fairly easy - I'd say the main issue is loss of local variables in async stack frames (which makes sense, unless they're closed they're gone), but that's not typically too big of a concern IMO.
- eska 6y agoDebugging those got a lot better in the new Firefox dev tools.
- happytoexplain 6y agoAbsolutely. I don't even write JS and I couldn't understand what point he was trying to make until he said it: That it was somehow not obvious that `await`... awaits?
- brundolf 6y agoI think many newer developers, especially ones who are naive to asynchronous programming, may learn/be taught that "when you want to get a result from an API, you just need to put 'await' in front of it", without fully understanding what exactly it entails. There may be fewer people in that situation on HN than there are in the developer population at large, but I theorize that it's a widespread issue and I wanted to shed some light on it.
- TheCoelacanth 6y agoThat's true, but in my experience, they also just memorize the ".then(result =>" pattern and make exactly the same mistake.
- brundolf 6y agoBut I maintain that in the illustrated case - where you want to get all the different independent values and then combine them at the end - it's actually less ergonomic to do it the wrong way with .then(...) than it is to do it the right way.
- jakear 6y agoMost ergonomic IMO is: const getPeople = async () => (await Promise.all(...)).flat() Without await this would be const getPeople = () => Promise.all(...).then(arrs => arrs.flat()) The difference is slight to be sure, but I usually prefer the await approach.
- faizshah 6y agoI did have this misunderstanding when relearning Promises and Async/Await in JS. The misunderstanding is actually not about await but about when a Promise is executed. The question you ask yourself when learning Promises is why calling await on a promise directly vs calling it on a variable containing a promise is different. Of course the root cause of the misunderstanding is not understanding Promises are executed immediately and await blocks on a promise. So creating a promise after an await causes the second promise to be executed after the awaited promise is resolved. For me I still had my mental model from a lazy execution environment (Dask) and that’s where my misconception came from.
- jakear 6y agoThanks for the contrarian perspective. Agree that if ones mental model of JS execution is flawed there will be confusion, but I’m not sure that is really reason to “beware of async/await”.
- akiselev 6y agoIt's a common problem with many languages - I've experienced it recently myself with Rust's futures. It requires you to be explicit about spawning tasks since that requires a heap allocation and it took me a while to figure out that I had to explicitly include an async runtime (of which there are several) and start up the async tasks myself. That said, I agree with the GP. I immediately saw that it was going to execute sequentially and thought nothing of it. Most of the time I use async/await not for concurrency but for syntax sugar and to get proper exceptions from promise based APIs instead of the GOTO FAIL control flow breaking nonsense that is catch().
- steveklabnik 6y agoIt doesn't inherently require a heap allocation, but the common, non-embedded use case does.
- akiselev 6y agoYeah I was glossing over the specifics for this context. The way futures/generators are implemented as syntax sugar over a closure/state machine that can live on the stack is one of my favorite features of Rust, but i.e. an executor that uses a smallvec task queue is a bit esoteric for a discussion that started around Javascript.
- thiht 6y agoI never used async/await as I'm more comfortable with promises so yeah, I thought the first code was correct. async/await is clearer to me after reading this article though.
- zenexer 6y agoIt was immediately clear that it was sequential. Given that the name of the function is “getPeople,” I figured that was probably the mistake the author was pointing out, but it was quite obvious. And I’m not really a JavaScript developer. This is a fairly common mistake, but it’s also easy to spot.
- azangru 6y ago> Can you spot the problem with this piece of code? Sure. There's no error handling :-) (Also, if it's not some magic fetch, the return value needs to be json-ed.)
- wereHamster 6y agoThere doesn't need to be, if you expect the caller (or the top-level code) to handle errors. Also, how should the function handle errors? It's not immediately obvious. An empty array is not the correct thing to return (an empty array could be a valid return value). Returning undefined/null would be another option, but that hides the error from the caller (which might be interested in the exact error message, to be able to properly deal with it).
- jakear 6y agoSimple! try { ... } catch (e) { throw “an unknown error occurred” } ;)
- mrspeaker 6y agoThat's what I thought they were going for at first too! "What's wrong with this code? It tries to make `async` look more readable by skipping the ugly try/catch block/s" ;)
- gamesbrainiac 6y agoI miss the defer keyword in coffeescript.
- fungifam 6y agoClickbait title. Beware of not reading the docs and making noob mistakes.
- onion2k 6y agoI hate that await has made it easy to turn JS in to a synchronous language. I see so much front end code that would be better for users without await, but lots of developers prefer to write code that's easier to think about rather than fast. It's hugely annoying.
- rectang 6y agoBut very little code has to be fast, right? Best to prioritize making code "easier to think about" and thus more likely correct, then to profile and optimize the bottlenecks.
- nine_k 6y agoAll the code in the UI should better be fast, else the UI drowns in annoying molasses real quick. So no, performance does matter a lot here.
- rectang 6y agoI don't dispute that UI code needs to be fast overall, but it's still the case that only a small proportion of the code needs to be fast in order for the whole thing to be fast. FWIW I worked in the search/database space for a decade and it's the same thing there. There was one time I wrote a lock-free hash table implementation for a registry that needed to be optimized for concurrent access; that code was much harder to write and to understand than the rest of the codebase, but it was justified. Most of the codebase, though, was not performance-critical. EDIT: I will grant that you still need to think about how to architect the project so that it is optimizable later, which can mean thinking about what needs to be async early on.
- onion2k 6y agoNot really. Front end devs should be aiming for a solid 60fps, so you only have about 16ms to do everything you need to do in a frame (less actually because the browser itself needs time as well). If you have interactions, network requests, animation, and calculations all blocking the main thread it'll be a horrible experience for the user. Giving up async and not letting the browser do what it can as soon as possible is just silly.
- Etheryte 6y agoWhile the idea is correct, the proposed solution is needlessly verbose. A better solution would be to async function getPeople() { const [members, nonMembers] = await Promise.all([ fetch("/members"), fetch("/non-members"), ]); return members.concat(nonMembers); } The gist of the article is very true though. This is something that comes up every now and then in code review, even with seasoned developers. I'm not entirely sure what the solution would be since depending on the context, both sequential and parallel execution can be correct. It might just be one of those rite of passage type of mistakes you have to make once or twice.
- jakear 6y agoOr if your env supports Array#flat, const getPeople = async () => (await Promise.all(...)).flat()
- bryanlarsen 6y agoor async function getPeople() { const membersPromise=fetch("/members"); const nonmembersPromise=fetch("/non-members"); return [].concat(await membersPromise, await nonmembersPromise); } edited to fix typo spotted by lhorie. thanks.
- d1plo1d 6y agoI expect that will execute sequentially though as opposed to the solution above it.
- abhishekjha 6y agoYou are not supposed to await immediately on the async tasks. Let them get fired in parallel without the await. Apply await only when you need to see the resolve/reject results.
- xixixao 6y agoHack has a more readable "fix" now, which will hopefully make it to JS in due course: async function getPeople() { concurrent { const members = await fetch("/members"); const nonMembers = await fetch("/non-members"); } return members.concat(nonMembers); }
- jakear 6y ago...why? Please, please, don’t add syntax to “fix” semantics that can already be elegantly expressed using existing constructs (Promise.all). Minimal-ish syntax is one of the few areas JS gets things right.
- xixixao 6y agoYour argument can be applied against await/async in general - it doesn’t allow anything Promises didn’t allow before. The reason is that it’s easy to make a mistake when destructuring the Promise.all call, and the Promise.all call doesn’t scale well (here dummy example, spot the bug) const [foo, baz, bar] = Promise.all([getFoo(), getBar(), getBaz()]) This is much more readable and less error prone: concurrent { const foo = await getFoo(); const bar = await getBar(); const baz = await getBaz(); }
- jakear 6y agoThere’s always a line and it must be drawn somewhere. Async/await allow for chaining deferred execution blocks without introducing deep syntactic nesting, at the cost of 2 keywords. In my opinion the benefits of flattening the language outweigh the costs of adding the keywords. The hack approach otoh introduces an entirely new syntax construct (tagged blocks), which declares variables that escape their initialization block (unexpected in the JS world post-let/const), and strongly leverages the existing hack execution model whereby all await’s in a single statement are executed concurrently. This execution model is incompatible with the rest of JS (hack disallows many styles of having multiple awaits in an expression, such as `await a && await b`), and at the end of the day what are the benefits? It executes a series of statements in parallel, exactly the same as Promise.all. If you’re concerned about mixing and matching declarations, maybe try let foo, bar, baz; await Promise.all([ foo = getFoo(), bar = getBar(), baz = getBaz()]) ? Edit: realized in the above example the variables remain of promise type, which isn’t great. One could alternatively do “getFoo().then(result => foo = result)”, but I agree this isn’t great either.
- recursive 6y agoThe way I would write the code is never covered, and I can't tell it's because I don't understand async, or I'm secretly a genius. The article says this is correct. > const both = await Promise.all([ members, nonMembers ]); > return both[0].concat(both[1]); I would have written it this way without the third promise. > return (await members).concat(await nonMembers); Is this wrong somehow?
- ajkjk 6y agoAfaik that just runs in serial, like the original example. `await` pauses execution immediately.
- recursive 6y agoYes, but the first pause comes after both requests have been started.
- ajkjk 6y agoAre you sure? It seems incongruous with my mental model, which is that the first `await`, when executed, immediately pauses execution, so the second is only dispatched when the first has resolved.
- recursive 6y agoYes. > the first `await`, when executed, immediately pauses execution Also true, but in my code, the first `await` happens after the second `fetch()` has already begun.
- DangitBobby 6y agoThe second await does not start until the first await resolves the first promise, but that doesn't mean network activity for the second initiated request isn't still happening in the background.
- DangitBobby 6y ago
- eat_veggies 6y agoSometimes (most of the time?) you want to run two async functions in series because the input of one function depends on the output of the other, or because you want the side effects of each to happen in the correct order. This is the whole point of promise chaining and the async/await sugar on top of it. Concurrency is hard, and I am glad that the default is something predictable but slow, rather than fast and racy.
- enedil 6y agoExcept the default is exactly fast and racy: fetch(x); fetch(y); vs await fetch(x); await fetch(y);
- tobyhinloopen 6y agoI thought it was about lack of error handling. Running them in parallel is not a solution. I would create a new API call to fetch both.
- spankalee 6y agoI thought this was going to be able stale data and race conditions, which is a much more insidious problem, IMO. The trifecta of mutability, shared state, and async operations can cause some very hard to reason about bugs.
- rough-sea 6y agoThe mistake is not getting the body of the response. Is it even meaningful to concat two response objects. It's amazing what gets upvoted on HN.
- uDontKnowMe 6y agoWould this not also work? async function getPeople() { const members = fetch("/members"); const nonMembers = fetch("/non-members"); return (await members).concat(await nonMembers); }
- deleted 6y ago[deleted]
- brundolf 6y agoInteresting, it actually does. I agree that's cleaner than the Promise.all() pattern.
- deleted 6y ago[deleted]
- _greim_ 6y agoOf the potential footguns with async/await, I'd rate this as low to non-existent. This falls into the category of straightforward code which gets a task done and which hasn't been optimized to within an inch of its life.
- torartc 6y agoWhy is this article so highly upvoted? The problem clearly isn't with Async/Await itself.
- happytoexplain 6y agoThis is what's confusing me - I have no problem with the complaint that a language feature is confusing or unfriendly to beginners, but in this particular case, why would somebody who doesn't know what await does even use it? What are they expecting it to do besides wait?
- asimpletune 6y agoI’ll admit that I don’t know a lot about JavaScript, but it’s a bit surreal coming from a Scala background and reading this blog. Like, I literally couldn’t understand how someone could make this mistake, until I remembered that there’re no types in JS. On that same note, Rust takes things even one step further and adds a “lifetime” axis, which if you’re used to then a similar blog post about memory access issues would be met with equal confusion about the author’s confusion. Hope that made sense.
- andyjpb 6y agoI'm not a JS programmer so the intended "simultaneous" waits only vaguely registered as possibly problematic. However, after reading the article it is, at worst, a performance problem. ...and performance should come after the code is correct and I don't think the code is even semantically correct in the first place. I'm assuming that "members" and "non-members" are distinct subsets of all People. i.e. it's not possible to be in the "members" and "non-members" sets at the same time. Additionally, everyone is in one or the other. Because the set membership is queried with 2 independent queries (designed in a reasonable and common RESTful manner) there is a race condition where an object (that changes in the time between the two calls) might appear in neither or both of the sets. These results are then blindly concatenated together. If the API wants to retain its RESTful design, it must include metadata about the consistency between calls. For example some kind of token that can be compared to check that the replies to the two queries represent a consistent view of the data. If the tokens, differ, the "transaction" can be retried. Alternatively, the API can be designed, possibly making it less RESTful in the process, so that the transaction is implemented on the server. There are many other ways to guarantee a correct and consistent result. Either change has big enough implications that optimising for performance at this stage would be premature because of the amount of refactoring necessary to implement `getPeople` in a way that provides consistent and correct semantics.
- brundolf 6y agoYou make a valid point, which I probably should have mentioned, that this doesn't apply for stateful calls. But in my experience it's a common use-case (at least on the front-end) to grab two or more independent datasets, from stateless endpoints, at the same time, for display purposes. And putting those into a sequence can have a dramatic impact on perceived performance. I don't think a performance factor of 2x or more is a micro-optimization that one should hold off on making until a later pass.
- justsomeuser 6y agoAnother issue: you could end up with duplicate people in the set if they are moved ‘member <-> !member‘ between each read. What you would need is a single HTTP request that uses a DB read transaction to get the whole set of people.
- tpetry 6y agoThe interesting point about this example is that an intelligent js compiler could easily rewrite this code into parallel execution. Its possible because the low-level await statement is declaring to wait on the execution but the calculated value is used much later first. Maybe someone could built this for typescript, the strict typesystem should make this easily (?) solvable by some hidden wrapper type, like AwaitedButUnaccesedPromise.
- Etheryte 6y agoThe problem is it may be intentional to have the requests be sequential depending on what you're doing. It would be straightforward to identify this kind of code, but the compiler wouldn't be able to tell which was the correct business logic.
- brundolf 6y agoYes, as another commenter pointed out, this would get you into trouble if the endpoint you're calling is stateful at all. And that's definitely not something a compiler could ever reason about.
- tpetry 6y agoOh thats true, didnt think about it. But this problem could be easily solved by adding a „defer“ keyword which is basically await under the hood with the added compiler optimizations.
- Smaug123 6y agoThe clue is even in the name. What do you expect to do on the keyword "await" except stop and wait?
- brundolf 6y agoAuthor here: lots of people are weighing in saying this is a micro-optimization and/or an uncommon case (making independent, stateless requests in parallel). My suspicion is that these people are not front-end programmers (a factor that I hadn't considered as making a difference, when writing this post). On the server, maybe having a set of requests take two seconds instead of one second (without consuming extra CPU cycles) doesn't really matter. On the front-end, it matters a whole lot. People go to much greater lengths to shave off much smaller amounts of time. Otherwise people on orange websites complain about your site being slow ;) On the server, maybe it's a really common case to be making requests that modify state, and/or to be stitching together several microservices that require IDs or other information to be passed between them. On the front-end, especially on first-load, you're often loading up several different datasets purely for display purposes. These are rarely written to depend on each other, because of the latency issue in my previous point. It is interesting though to see that there's a difference in norms/use-cases between the two.
- TheCoelacanth 6y agoI don't think it's so much that a second is more significant on the front-end. An entire second is practically an eternity on the backend. The main difference is how much latency a typical request is likely to have. On the backend, this optimization would frequently only save you 50 ms, because you are making a low latency call to another service that you are running in the same datacenter.
- brundolf 6y agoThat's a good point. And I guess the payloads will often be smaller, because the large datasets often just stay in the DB until needed. Whereas if the front-end needs to load data for a huge table (from a location that's obviously not in the same datacenter), it's easy for this optimization to save you hundreds of milliseconds if not entire seconds.