Yes. Use require('fs/promises') or require('fs').promises. These provide promise-based versions of fs methods (readFile, writeFile, etc.). No need to promisify manually.
Does Node.js have built-in promise-based APIs for fs in JavaScript?
More FAQs in Promisifying Callback-Based APIs in JavaScript
Return new Promise((resolve, reject) => { callbackApi(args, (err, result) => { if (err) reject(err); else resolve(result); }); }). The callback must follow the error-first convention.
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 with .then or async/await.
function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }. Then use await delay(1000) to pause. Node.js provides timers/promises with setTimeoutP 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.