var content;
fs.readFile('./Index.html', function read(err, data) {
if (err) {
throw err;
}
content = data;
});
console.log(content);
日志未定义,为什么?
var content;
fs.readFile('./Index.html', function read(err, data) {
if (err) {
throw err;
}
content = data;
});
console.log(content);
日志未定义,为什么?
当前回答
为了详细说明@Raynos所说的内容,您定义的函数是一个异步回调。它不会立即执行,而是在文件加载完成后执行。当调用readFile时,立即返回控制,并执行下一行代码。因此,当您调用console.log时,您的回调还没有被调用,这个内容还没有设置。欢迎学习异步编程。
例子的方法
const fs = require('fs');
// First I want to read the file
fs.readFile('./Index.html', function read(err, data) {
if (err) {
throw err;
}
const content = data;
// Invoke the next step here however you like
console.log(content); // Put all of the code here (not the best solution)
processFile(content); // Or put the next step in a function and invoke it
});
function processFile(content) {
console.log(content);
}
或者更好的是,如Raynos示例所示,将调用包装在函数中,并传递自己的回调。(显然这是更好的实践)我认为养成将异步调用包装在接受回调的函数中的习惯将为您节省大量麻烦和混乱的代码。
function doSomething (callback) {
// any async callback invokes callback with response
}
doSomething (function doSomethingAfter(err, result) {
// process the async result
});
其他回答
在ES7中使用Promises
与mz/fs异步使用
mz模块提供了核心节点库的承诺版本。使用它们很简单。首先安装库…
npm install mz
然后……
const fs = require('mz/fs');
fs.readFile('./Index.html').then(contents => console.log(contents))
.catch(err => console.error(err));
或者你也可以在异步函数中写它们:
async function myReadfile () {
try {
const file = await fs.readFile('./Index.html');
}
catch (err) { console.error( err ) }
};
使用内置的promisify库(Node 8+)使这些旧的回调函数更加优雅。
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
async function doStuff() {
try {
const content = await readFile(filePath, 'utf8');
console.log(content);
} catch (e) {
console.error(e);
}
}
同步和异步文件读取方式:
//fs module to read file in sync and async way
var fs = require('fs'),
filePath = './sample_files/sample_css.css';
// this for async way
/*fs.readFile(filePath, 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
});*/
//this is sync way
var css = fs.readFileSync(filePath, 'utf8');
console.log(css);
节点欺骗可在read_file。
我喜欢使用fs-extra,因为所有函数都是承诺的,开箱即用,所以可以使用await。所以你的代码可以是这样的:
(async () => {
try {
const content = await fs.readFile('./Index.html');
console.log(content);
} catch (err) {
console.error(err);
}
})();
为了详细说明@Raynos所说的内容,您定义的函数是一个异步回调。它不会立即执行,而是在文件加载完成后执行。当调用readFile时,立即返回控制,并执行下一行代码。因此,当您调用console.log时,您的回调还没有被调用,这个内容还没有设置。欢迎学习异步编程。
例子的方法
const fs = require('fs');
// First I want to read the file
fs.readFile('./Index.html', function read(err, data) {
if (err) {
throw err;
}
const content = data;
// Invoke the next step here however you like
console.log(content); // Put all of the code here (not the best solution)
processFile(content); // Or put the next step in a function and invoke it
});
function processFile(content) {
console.log(content);
}
或者更好的是,如Raynos示例所示,将调用包装在函数中,并传递自己的回调。(显然这是更好的实践)我认为养成将异步调用包装在接受回调的函数中的习惯将为您节省大量麻烦和混乱的代码。
function doSomething (callback) {
// any async callback invokes callback with response
}
doSomething (function doSomethingAfter(err, result) {
// process the async result
});