Wrap it in a new Promise. Call the original function with a callback that calls reject on error and resolve on success. Return the promise. The callback must follow the error-first convention: (err, result) => {}.
How do you convert a callback-based function to a promise in JavaScript?
More FAQs in Converting Callbacks to Promises in JavaScript
A built-in utility that converts a callback-based function (error-first convention) to a promise-based one. Usage: const readFileP = promisify(fs.readFile). Then use it with .then or async/await.
Yes. Many APIs have promise-based versions. Use require('fs').promises or require('fs/promises') for file system, for example. No need to promisify manually. Use them with async/await.
Use new Promise with emitter.once: new Promise((resolve, reject) => { emitter.once(event, resolve); emitter.once('error', reject); }). Node.js provides events.once(emitter, event) for this.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.