问题
#!/usr/bin/env node
var fs = require('fs')
, async = require('async')
, program = require('commander')
program
.version('0.0.1')
.usage('<keywords>')
.parse(process.argv)
async.waterfall([
fs.readdir.bind(fs, __dirname),
parseHTML,
], saveResult)
function parseHTML(files, callback) {
var result = []
files.forEach(function(file) {
if (file.match(/\.html$/)) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err
result.push(data)
})
}
})
callback(null, result)
}
function saveResult(err, result) {
console.log(result)
}
I'm confused because console.log(data)
does output the data:
<p>File 1</p>
<p>File 2</p>
Yet the final result
is an empty array: []
Why am I doing wrong?
回答1:
You have to wait to look at the result until the last fs.readFile()
operation has finished. These are async operations and they complete some time in the future. You are examining the result before any of them have finished.
There are many ways to approach solving this, but this method would likely cause the least change to your code as it just keeps a counter of how many are done:
function parseHTML(files, callback) {
var result = [],
cntr = 0;
files.forEach(function(file) {
if (file.match(/\.html$/)) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err
result.push(data)
// see if we're done processing all the results
++cntr;
if (cntr === files.length) {
callback(null, result);
}
});
} else {
++cntr;
if (cntr === files.length) {
callback(null, result);
}
}
});
}
I'd personally prefer to use promises and Promise.all()
to solve this.
Here's a version using the Bluebird promise library that retains some of your other structure:
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
// your other code here
function parseHTML(files, callback) {
var promises = [];
files.forEach(function(file) {
if (file.match(/\.html$/)) {
promises.push(fs.readFileAsync(file, 'utf8'));
});
Promise.all(promises).then(function(results) {
// all results in results array
callback(null, results);
}, function(err) {
// error here
});
}
And, here's a fully promise version:
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
function parseHTML(files) {
var promises = [];
files.forEach(function(file) {
if (file.match(/\.html$/)) {
promises.push(fs.readFileAsync(file, 'utf8'));
});
return Promise.all(promises);
}
fs.readdirAsync(__dirname).then(parseHTML).then(function(results) {
// files are in the results array here
}).catch(function(err) {
// error here
});
来源:https://stackoverflow.com/questions/29051467/why-is-this-fs-readfile-loop-not-pushing-its-results-to-my-array