I have a code like this below. The code still use callback function and i want to change using promise. I hesistate how to change them. can you help how change it
const fs = require('fs')
fs.readdir('/', (err, result) => {
if (err) {
throw new Error(err.message)
}
console.log(result)
})
Provided that you import asycronous version of readdir() which is located in require('fs').promises you can read directories asyncronouly
const fs = require('fs').promises
const asyncReadDir = async(dirName) => {
try {
const dirData = await fs.readdir(dirName)
console.log("Read successfully")
// other code here
} catch(error){
// catch errors
console.log(error)
}
}
asyncReadDir("/downloads")