文件结构图
{ "next":"b.json", "msg":"this is a" }
{ "next":"c.json", "msg":"this is b" }
{ "next":"null", "msg":"this is c" }
上一层
const fs=require('fs') const path=require('path') //用jpromise获取文件内容 function getFileContent(filName){ const promise= new Promise((resolve, reject)=>{ const fullFileName=path.resolve(__dirname,'files',filName) fs.readFile(fullFileName,(err,data)=>{ if(err){ reject(err) return } resolve( JSON.parse(data.toString()) ) }) }) return promise } getFileContent('a.json').then(aData=>{ console.log("a data", aData) return getFileContent(aData.next) }).then(bData=>{ console.log("b data", bData) return getFileContent(bData.next) }).then(cData=>{ console.log("c data", cData) })