This guy uses await. Look how happy he is. Are you aware of how awesome the new es7 feature is ? Showtime ! async/[await](https://hackernoon.com/tagged/await) 1. At the beginning, there was callbacks Imagine you are told to order an amazon package, then go grab it at the post office, then come back home to open it. Simple instructions. Well guess what, most people do not spend 3 days waiting in front of the post office for their package to arrive. Apart the fact that you would probably starve, this doesn’t look like a very efficient way of organizing your workflow. Similar to waiting for an amazon package, the web development is mostly about sending server/database/services request, then wait for it to come back, and do something with the data. It is thus only natural to use a language that is capable of dealing with concurrency. The callbacks are a way to deal with concurrency. Node users are familiar with code that reads It basically tells to fetch a user from some database, while passing two parameters: the , your query userId a.k.a the callback function that explains what to do As a node convention, the first callback argument is an error message, whose you hope it’s just (err, user) => {...} when the query results arrive. null It works, but at some point it becomes tedious to deal with all the error handling, and you’ll probably end up with some incomprehensible, deep-nested code. Many speak of this situation as the . I’m not quite sure it’s a , but it definitely looks boring to me. callback hell hell 2. Enter Promises Promises are more recent feature that helps chaining asynchronous tasks, while keeping a better readability. Our first example would now read Wow, much better ! But what if I want to use both and as arguments to another function ? This would turn our code into user result Aaaand we’re back to multiple nesting levels. How sad :( Promises are a great thing, and they allowed a tremendous improvement of code readability. They do not however solve our problems, and nesting/error catching still exists, although it is definitely better than with callbacks. all EDIT: It has been pointed out by clever people that a more honest/elegant way of solving the example2 with promises could read: It is indeed better than my original solution :) 3. Enter Await Brace yourselves, ES7 is coming. At the moment is still a proposal, so the following could be subject to (minor I hope) changes. async/await To use ES7 features, either , or use (or anything else I didn’t think to mention). As for today, the TS team chose to release a feature, that has the same specs as its ES7 counterpart. Note that this might change someday, though we hope not. setup babel typescript await/async First, let’s update our example: Here’s a few things to notice: The function is declared so we can use inside the function async await will work as if you were developing in a synchronous world. Although it does NOT block the thread (so it is actually asynchronous), it allows the developer to really express what’s he/she is thinking about: do stuff, name the result, and use them later. const user = await User.findById(userId) Back to good ol’ for error handling try/catch Examples of cool stuff we can do with await/async Promisify An async function always return a promise. You can even use it to “promisify” a regular function: Notice how declaring the function as turned (a number) into a async return i +1 Promise<number> Throw A will return a promise that reject on error: throw Another example ? Sleep Implementing the function: sleep Node.js test example How about we look at three ways of writing a test ? nodejs This test count the number of users to check there is more than 1, then fetch one, and check it has an field. email Which one looks better to you ? Protip: parallelism will wait for multiple parallel promises to resolve: await Promise.all(...) Conclusion If you’re still here, then I’m pretty sure you now have a decent idea of what’s going on with this much fancy feature. async/await I hope you’re now convinced of how awesome it is. Let me know what you think !
Share Your Thoughts