var fs =require('fs') fs.readFile('./a.txt', 'utf8',function(err,data){ if(err){ console.log('读取失败') }else{ console.log(data) fs.readFile('./b.txt', 'utf8',function(err,data){ if(err){ console.log('读取失败') }else{ console.log(data) fs.readFile('./c.txt', 'utf8',function(err,data){ if(err){ console.log('读取失败') }else{ console.log(data) } }) } }) } })
var fs =require('fs') var p1 =new Promise(function(resolve,reject){ fs.readFile('a.txt','utf8',function(error,data){ if(error){ reject() console.log('读取失败') } else{ resolve(data) } }) }) var p2 =new Promise(function(resolve,reject){ fs.readFile('./b.txt','utf8',function(err,data){ if(err){ reject() console.log('读取失败') }else{ resolve(data) } }) }) var p3 = new Promise(function(resolve,reject){ fs.readFile('./c.txt','utf8',function(err,data){ if(err){ reject() console.log('读取失败') }else{ resolve(data) } }) }) p1.then(function(data){ console.log(data) return p2 },function(err){ console.log('读取失败') }).then(function(data){ console.log(data) return p3 },function(err){ console.log('读取失败') }).then(function(data){ console.log(data) },function(err){ console.log('读取失败----------') })
var fs =require('fs') function readFireFun(url){ return new Promise(function(resolve,reject){ fs.readFile(url,'utf8',function(err,data){ if(err){ console.log('读取失败') reject(err) }else{ console.log(data) resolve(data) } }) }) } readFireFun('./a.txt').then(function(data){ return readFireFun('./b.txt') }).then(function(data){ return readFireFun('./c.txt') })