Skip to content Skip to sidebar Skip to footer

Why Is This Fs.readfile Loop Not Pushing Its Results To My Array?

#!/usr/bin/env node var fs = require('fs') , async = require('async') , program = require('commander') program .version('0.0.1') .usage('') .parse(proc

Solution 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:

functionparseHTML(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:

varPromise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));

// your other code herefunctionparseHTML(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 arraycallback(null, results);
    }, function(err) {
       // error here
    });
}

And, here's a fully promise version:

varPromise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));

functionparseHTML(files) {
    var promises = [];

    files.forEach(function(file) {
        if (file.match(/\.html$/)) {
            promises.push(fs.readFileAsync(file, 'utf8'));
    });
    returnPromise.all(promises);
}

fs.readdirAsync(__dirname).then(parseHTML).then(function(results) {
    // files are in the results array here
}).catch(function(err) {
    // error here
});

Post a Comment for "Why Is This Fs.readfile Loop Not Pushing Its Results To My Array?"