paint-brush
6 Reasons Why JavaScript Async/Await Blows Promises Away (Tutorial) by@mgaafar
464,975 reads
464,975 reads

6 Reasons Why JavaScript Async/Await Blows Promises Away (Tutorial)

by Mostafa GaafarMarch 16th, 2017
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Async/await is just syntax sugar built on top of promises. NodeJS supports it out of the box since version 7.6.6. It has been the single greatest addition to JS since 2017. Here are a bunch of reasons with examples why you should adopt it immediately and never look back. It makes asynchronous code look and behave a little more like synchronous code. The advantages add up quickly, which will become more obvious in the following code examples. We didn’t have to write an anonymous function to handle the response. Error handling makes it finally possible to handle both synchronous and asynchronous errors with the same construct.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - 6 Reasons Why JavaScript Async/Await Blows Promises Away (Tutorial)
Mostafa Gaafar HackerNoon profile picture
NodeJS supports async/await out of the box since version 7.6. I believe it has been the single greatest addition to JS since 2017. If you haven’t tried it yet, here are a bunch of reasons with examples why you should adopt it immediately and never look back. [UPDATE]: This article has been edited to be more relevant in 2019

Async/Await 101

For those who have never heard of this topic before, here’s a quick intro
  • Async/await is a new way to write asynchronous code. Previous alternatives for asynchronous code are callbacks and promises.
  • Async/await is actually just syntax sugar built on top of promises. It cannot be used with plain callbacks or node callbacks.
  • Async/await is, like promises, non blocking.
  • Async/await makes asynchronous code look and behave a little more like synchronous code. This is where all its power lies.

Syntax

Assuming a function getJSON that returns a promise, and that promise resolves with some JSON object. We just want to call it and log that JSON, then return "done".

This is how you would implement it using promises

2. If you set a breakpoint inside a .then block and use debug shortcuts like step-over, the debugger will not move to the the following .then because it only “steps” through synchronous code.

With async/await you don’t need arrow functions as much, and you can step through await calls exactly as if they were normal synchronous calls.

In Conclusion

Async/await is one of the most revolutionary features that have been added to JavaScript in the past few years. It makes you realize what a syntactical mess promises are, and provides an intuitive replacement.

Concerns

Some valid skepticism you might have about using this feature is that it makes asynchronous code less obvious: Our eyes learned to spot asynchronous code whenever we see a callback or a .then, it will take a few weeks for your eyes to adjust to the new signs, but C# had this feature for years and people who are familiar with it know it’s worth this minor, temporary inconvenience.

Follow me on twitter
바카라사이트 바카라사이트 온라인바카라