Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda