Shift from Callback hell to async/await heaven

Yogendra Saxena
Sep 2, 2018 · 3 min read

So being a javascript developer I “always” prefer using async/await instead of callbacks.

Every javascript developer knows this pretty well that nested callbacks can make you crazy.

So , As a new Coder You can always prefer async/await but what about the libraries which is already build using callback. You do not have any choice but to use Callbacks.

What if the code written by your senior supports only callback , and that particular function is been referred all over the code base ,then changing that one callback function into promise will be a nightmare .So this might be a worst case where you will get trapped into callback hells.

So whats the Solution..

Let me demonstrate it by using a very simple example of Adding 2 number

So lets look at callbacks.

Here you can see the response as

Hi my name is yogendra

I will find sum of 2 number

So finally the sums is 5

Now this looks fine and clean because it has only one callback , it doesnt have nested callbacks

A developer might go crazy if it will contains a lot of nested callbacks

Now let see how you can convert this callback into Promise and then start using async/await in that

Yes We have a Core Node module called “util” , It has a capability to convert any callbacks into promises.

util is a fully-featured Promise library for JavaScript. The strongest feature of util is that it allows you to “promisify” other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions. This concept is used to ensure that every callback function which is called returns some sort of value.

So if a Node JS module contains a callback function which does not return a value, if we Promisify the node module, all the function’s in that specific node module would automatically be modified to ensure that it returns a value.

How to implement it?

Taking same example and converting in into promise

All magic happens here in this line

addme = promise.promisify(addme);

this particular line convert callbacks in to promise

Things to Note:

It only handles callbacks which is written in NodeJS Standard way of callback.

“Like last parameter should be a callback function , and this callback functions 1st parameter should be err and 2nd should be response”

This is the most Usefull NodeJS Core module I came accross so far.

It has a capability to convert whole library accepting callback to promise using promisifyAll()

var mongoClient = Promise.promisifyAll(require('mongodb')).MongoClient;

There are few more 3rd party module which perform same task more or less than this ‘util’ module , one of this 3rd party module is bluebird

This was just a simple example, but the very same concept you can use in any complex callbacks and it will definitely work.

Thanks for Reading

Happy Learning :)

FullStack Engineer working with JavaScript building scalable backend using NodeJS . https://github.com/yogain123

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade