Vikas Patel     Sep,27 2017

Async/Await, Modern way of writing nodejs code

The biggest problem with understanding the nodejs  code is to follow the callbacks. Probably reading out someone else's code is the hardest job for any developer. Nodejs adopted to write down callbacks for each and every task to achieve maximum parallelism. But this is the most pinching part of any newbie to understand.

 

Most of the beginners do not realize the sequence of below code:

 

 

In reality, most of the time you will need parallelism when iterating over an array. Probably the best way is to use the `eachLimit` method of the `async` module link. Again, looping will also have subtask with their own callbacks, and that will again increase the complexity. For example, you have a bunch of physical paths and you want to read each file and replace some characters and then write them again.  This is the classic case that we can read files parallelly but we can write the files only after the read process is completed.

 

People also use the promise libraries like `q` and `bluebird`, to chain some tasks but again, most of the item you can not chain all your tasks. Eventually, you'll realize that writing sequential code was much easier and simple to traverse.

 

Async/Await Nodejs

 

async/await is a new way of writing code. You can use it with the help of promises.  For example, I have used bluebird for promisifying the google map module. Notice that I've also promisify the setTimeout function for delayed processing.

 

 

Node developers can have much better async/await flow for Node 8 or Newer version as it is fully supported.

 

Node.js library async/await offer own cross-platform versions of the feature.

 

Async/Await is now available in all new versions of most major browsers, except IE11. All other browsers recognize async/await code without the need of external libraries.