目录

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
上次更新: 2022-12-19 20:25:37