Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!