从fs.readFile获取数据var content;fs.readFile('./Index.html', function read(err, data) {
if (err) {
throw err;
}
content = data;});console.log(content);原木undefined为什么?
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
const fs = require('fs');var content;// First I want to read the filefs.readFile('./Index.html', function read(err, data) { if (err) { throw err; } content = data; // Invoke the next step here however you like console.log(content); // Put all of the code here (not the best solution) processFile(); // Or put the next step in a function and invoke it});function processFile() { console.log(content);}
function doSomething (callback) { // any async callback invokes callback with response}doSomething (function doSomethingAfter(err, result) { // process the async result});
温温酱
TA贡献1752条经验 获得超4个赞
function readContent(callback) { fs.readFile("./Index.html", function (err, content) { if (err) return callback(err) callback(null, content) })}readContent(function (err, content) { console.log(content)})
添加回答
举报
0/150
提交
取消