Promise
# Promise 封装 fs 读取文件
const fs = require('fs')
function myReadFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err)
} else {
resolve(data.toString())
}
})
})
}
myReadFile('./package.json').then(value => {
console.log(value)
}, reason => {
console.log(reason)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
编辑 (opens new window)
上次更新: 2025-01-11 19:08:58