如何把回调函数转为 Promise
fairyly opened this issue · 0 comments
fairyly commented
如何把回调函数转为 Promise
- demo
const readFilePromise = (...args) => {
return new Promise((resolve, reject) => {
fs.readFile(...args, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
}
# use
readFilePromise(filePath, options)
.then(data => {/* Do something with data */})
.catch(err => {/* Handle error */}