const fs = require('fs');
const path = require('path');
function promiseReadFile(path) {
return new Promise((resolve,reject)=>{
fs.readFile(path,(err,res)=>{
if (err){
reject(err);
return;
}
resolve(res);
});
});
}
async function asyncReadFile(path) { //我想在这个函数中直接return res怎么做到
let res = await promiseReadFile(path);
console.log(res.toString());
return res;
}
asyncReadFile(path.resolve(__dirname,'./data/1.txt')); //这样运行是可以的
// let res = asyncReadFile(path.resolve(__dirname,'./data/1.txt')); //我想把函数的返回值赋给res 打印结果是 [object Promise]
// console.log(res);
//求赐教!!!!!!!!!
const fs = require('fs');
const path = require('path');
function promiseReadFile(path) {
return new Promise((resolve,reject)=>{
fs.readFile(path,(err,res)=>{
if (err){
reject(err);
return;
}
resolve(res);
});
});
}
(async function(){ //上面的代码多走了一步 直接在async中调用就好 想法有问题
let res = await promiseReadFile(path.resolve(__dirname,'./data/1.txt'));
console.log(res.toString());
})();