node中Promise使用实例
需求:现有三个文件需要依次读取文件内容
// a.json
{
"next": "b.json",
"massage": "this is a file content"
}
b.json
{
"next": "c.json",
"massage": "this b file content"
}
c.json
{
"next": "null",
"massage": "this c file content"
}
目录结构:
实现方法如下:
// 方法一: callback获去文件的内容
const fs = require("fs")
const path = require("path")
// 定义一个函数去获取文件内容
function getFileContent(fileName, callback) {
const fullFilename = path.resolve(__dirname, "files", fileName)
fs.readFile(fullFilename, (err, data) => {
if(err){
console.error(err)
return
}