DEV Community

talent
talent

Posted on

Callback-based asynchronous functions

Let's face it. Sometimes you have to use an asynchronous function that uses the old callback-based approach. It might be part of a legacy codebase, or it could come from a library that hasn't had an update in over 5 years. 🤷‍♂️

So what do you do? Do you add messy callbacks to your code? 🙅‍♂️

What if I told you there is a way to make callback-based asynchronous functions work nicely with async/await? Not only that, but you won't have to make any changes to existing code.

The solution is to transform the callback-based function to return a promise. This called promisification. You can do that manually or by using util.promisify.

Image description
Image description

Top comments (0)