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

203
Views
Lack of execution order on forEach (async and await) to node js

I have an function for each await and it gives a string and adds it to an array, but the log that I got later shows the array is empty and it is done before the for each is done.

 const paths: Array<string> = [];
  await filterFiles.forEach((data, index) => {
    fileSave(data, `${name}@${index}`, National_Code, "medium", "image")
      .then((data: string) => {
        paths.push(data);
        console.log(paths);
        console.log("log 2");
      })
      .catch((err) => {
        res.status(500).json("There is a problem saving the files");
        return err;
      });
  });
  console.log(paths);
  console.log("log 1");

Result :

[]
log 1
[ 'image/Czech@0_0927735016.jpeg' ]
log 2
[ 'image/Czech@0_0927735016.jpeg', 'image/Czech@1_0927735016.jpeg' ]
log 2

What did I do wrong?

about 4 years ago · Juan Pablo Isaza
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!