文件结构图
{ "next":"b.json", "msg":"this is a" }a.json
{ "next":"c.json", "msg":"this is b" }b.json
{ "next":"null", "msg":"this is c" }c.json
上一层
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) })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。