**Promotion** - Efficiently manage your coding bookmarks, aka #codingmarks, on www.codingmarks.org and share your hidden gems with the world. They are published weekly on Github. You can help us build THE programming-resources location - Star
Some days ago, I presented how the code got much cleaner when I replaced Mongoose callbacks with the
async-await feature from ES7, which NodeJs already support.
I mentioned then, I was gonna do a post to demonstrate how
async-await can also be used to make parallel calls.
Here am I doing just that. I was actually blown away by its simplicity and performance gain when doing so.
Let me show you what I mean.
Imagine we have a notification service (might be a REST service) that sends emails to selected employees of an organisation -
The code before using parallelization is iterating through the list of employees and send sequentially emails:
With an actual notification service implementation it took around 40 seconds for 30 employees. This is definitely a NoGo in modern times…
The notification service might look something like the following:
Here is the same code, but now the emails are sent in parallel:
First of all the performance gain was fantastic: down to 1.3 seconds from 40 seconds…
It’s more or less how long it takes the notification service to respond to a call
This is made possible with the help of
function which aggregates the result of multiple promises:
- we define an array of Promises of type the result expected from the notification service
- we iterate over the employees of the company and push the call to the array defined above
awaitfor all the promises
- that’s it - it cannot get much simpler than that…
Stay tuned for other cool async-await features I might come across along the way…