What is the error-first convention for promisification in JavaScript?
The callback's first argument is an error (null if no error). The second argument is the result. promisify checks if (err) reject(err) else resolve(result). The original function must follow this convention.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Converting Callbacks to Promises in JavaScript
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) => {}.
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.
Still have questions?
Browse all our FAQs or reach out to our support team
