3 ms·
Async/await has been a game-changer. You can tell that this is the case by how much shorter and more reliable code with async/await is compared to alternatives
by jongjong 2y ago
Async/await has been a game-changer. You can tell that this is the case by how much shorter and more reliable code with async/await is compared to alternatives (e.g. which rely on callbacks).
Callbacks are a leaky abstraction IMO because they force you to write a lot of additional logic to handle results in the correct sequence. Obtaining the result of multiple long-running function calls on the same line is insanely messy with callbacks.
I've found that async/await helps to write code which stays close to the business domain. You can design it with multiple layers which take care of a distinct level of abstraction so you can understand what's happening just by reading the main file top to bottom. You don't have to jump around many files to read the functions to understand what they are doing and how their results are combined.
Async/await makes the concurrency aspect as succinct as possible.
That said, I've seen many people writing code with async/await as though they were writing using callbacks... Some people haven't adapted correctly to it. If your async/await code doesn't look radically different from how you used to write code, then you're probably doing something seriously wrong. Ensure that you familiarize yourself with the async/await utility functions of your language like (in JS) Promise.all, Promise.race... Notice how try-catch works with the await keyword...