Convert a Callback-Based JavaScript Function to a Promise-Based One

InstructorMarius Schulz

Share this video with your friends

Send Tweet

Sometimes, you might want to convert a JavaScript function that accepts a callback to one that returns a Promise object. This lesson shows how to manually wrap a promise-based API around the fs.readFile() function. It also explains how to use the util.promisify() method that is built into the Node.js standard library.

Martin
~ 2 years ago

I understand now how how to convert the callback-based function. But why should I do it in the first place? Only to use .then() and .catch(). What’s the benefit to do it?

Lucas Minter
~ 2 years ago

Hey Martin, I found this for you. I think this stack overflow should help you find the answer you're looking for.