Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
Node: How to async await folder file reads

How to read files asynchronously in node js, here is a simple function.

There are a lot of convoluted answers on the internet, does anyone agree if this is the simplest?

export default async function handler(req, res) {
  let data =  await readFiles('data/companies/');
  res.status(200).json(data);
}

// async file reader
function readFiles(dirname) {
  return new Promise(function (resolve, reject) {
    let data = {}

    fs.readdir(dirname, async function(err, filenames) {
      filenames.forEach(function(filename) {
        fs.readFile(dirname + filename, 'utf-8', function(err, content) {
          if (err) {
            reject(err)
          }
          data[filename] = content;

          if (filenames.length === Object.keys(data).length) {
            resolve(data)
          }
        });
      });
    });
  })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A bit cleaner and easier using the built in promise support in fs.promises:

const fs = require('fs');
const fsp = fs.promises;
const path = require('path');

// async file reader
async function readFiles(dirname) {
    const data = {};
    const files = await fsp.readdir(dirname);
    for (const filename of files) {
        const full = path.join(dirname, filename);
        const content = await fsp.readFile(full, {encoding: 'utf8'});
        data[filename] = content;
    }
    return data;
}

Or, if you want to run your file operations in parallel (at least to the limit of the thread pool), you might get slightly faster end-to-end performance like this:

// async file reader
async function readFiles(dirname) {
    const data = {};
    const files = await fsp.readdir(dirname);
    await Promise.all(files.map(async filename => {
        const full = path.join(dirname, filename);
        const content = await fsp.readFile(full, {encoding: 'utf8'});
        data[filename] = content;
    }));
    return data;
}

Also, this:

res.status(200).json(data);

can be replaced with:

res.json(data);

200 is already the default status so there is no reason to specify it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda