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

380
Views
Asynchronous file reading, cannot make function finish before returning

I am trying to read all files in folder, check if it has featured: true prop, increase the counter if it does and then throw error if there is more than 1 such file. But for some reason, my code doesn't wait and simply jumps to last section, this is the output

this should be last
Inside item, looking for featured prop
Inside item, looking for featured prop

My code:

const fs = require('fs');
const findInDir = require('./utils/findInDir');

(async () => {
  const dir = './public/page-data/blog';
  const fileRegex = /.*/;
  const allFiles = findInDir(dir, fileRegex);
  let result = 0;

  await allFiles.map(file => {
    fs.readFile(file, 'utf8', (err, data) => {
      const obj = JSON.parse(data);

      if (obj.result.pageContext.featured === true) {
        console.log('Inside item, looking for featured prop');
        result += 1;
      }
    });
  });

  console.log('this should be last');

  if (result > 1) {
    throw new Error('There are multiple featured blog posts, please fix.');
  }
})();

Need some help understanding what am I doing wrong.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to use the non-callback version of fs.readFile in order to actually await reading the file. Also using .map does not make a lot of sense as you're not actually mapping something, so simply use a for .. of loop:

for (const file of allFiles){
 try {
   const data = await fs.promises.readFile(file);
   // ...
 } catch(err) {
   // handle error
 }
}

console.log('this should be last');
about 4 years ago · Juan Pablo Isaza Report

0

Quick solution:

(async () => {
  const dir = "./public/page-data/blog";
  const fileRegex = /.*/;
  const allFiles = findInDir(dir, fileRegex);

  try {
    await Promise.all(
      allFiles.map((file) => {
        return new Promise((resolve, reject) => {
          fs.readFile(file, "utf8", (err, data) => {
            const obj = JSON.parse(data);

            if (obj.result.pageContext.featured === true) {
              reject("Inside item, looking for featured prop");
            } else {
              resolve();
            }
          });
        });
      })
    );
  } catch (err) {
    throw new Error("There are multiple featured blog posts, please fix.");
  }
})();

Instead of checking every item one by one like in an loop i do it like this. It checks "parallel" if any of them has featured set to true. Therefore its faster then a loop

If its true i reject it and the Promise.all() stops. The try / catch of the Promise.all() catches the rejection


You need to understand just 1 thing to know when to use await and when not.

It only makes sense to use await on promises.

How do you know if its an promise? Just console.log it

Does it console log Promise? Use await

Does it NOT console log Promise? It makes no sense to use await

enter image description here

This returns just an array. Does it makes sense to use await? No, it does not return an Promise

But if you do it like this:

enter image description here

You have an array of promises. Here is a bit different. You have an array BUT the elements inside are an promise. You can use Promise.all() it basically awaits for every item in the array until its done.

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!