2

I'm trying to read all the files inside a specific folder.

But I'm having some issues returning an array with all those files' data.

My function is returning an empty array because the return is called before all values have been pushed into the array.

How can I fix this problem using asynchronous mechanisms?

app.get('/load-schemas', async function (req, res) {
  var schemas = [];
  fs.readdirSync('Schemas').forEach(file => {

    fs.readFile('Schemas/' + file, "utf8", function(err, data) {
      schemas.push(data);
    })
  });
  
  res.status(200).send(schemas);
})
Tim
  • 5,435
  • 7
  • 42
  • 62
sw gdois
  • 45
  • 5

3 Answers3

4

I guess the easiest solution is to go with readFileSync

let data = fs.readFileSync('Schemas/' + file, "utf8");
schemas.push(data);
bill.gates
  • 14,145
  • 3
  • 19
  • 47
1

Since you can use async/await in your code I would use the promises from fs and wait them like here https://stackoverflow.com/a/58332163/732846

This way the code looks like "sync" code but has the benefits of being async

const { promises: fs } = require("fs");

app.get('/load-schemas', async function (req, res) {
  var schemas = [];
  const dirs = await fs.readdir('Schemas');
  dirs.forEach(file => {
    const data = await fs.readFile('Schemas/' + file, "utf8");
    schemas.push(data);
  });

  res.status(200).send(schemas);
})
Edub
  • 508
  • 7
  • 24
0

I think that you can go for promises.

snippet from: How do I wait for multiple fs.readFile calls?

const fs = require('fs');

function readFromFile(file) {
    return new Promise((resolve, reject) => {
        fs.readFile(file, function (err, data) {
            if (err) {
                console.log(err);
                reject(err);
            }
            else {
                resolve(JSON.parse(data));
            }
        });
    });
}

const promises = [
    readFromFile('./output/result3.json'),
    readFromFile('./output/result4.json')
];

Promise.all(promises).then(result => {
    console.log(result);
    baseListOfFiles = result[0];
    currentListOfFiles = result[1];
    // do more stuff
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#:~:text=The%20Promise.,input%20iterable%20contains%20no%20promises.

tstoev
  • 1,415
  • 11
  • 12