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

132
Views
How to wait for all in Javascript

My code is nearly working well my problem is that only the first CSV data is logged and the lambda function ends.

I guess I need someway to wait for all stream pipes to end.

myCsvList.forEach((myElem) => {
      const data = [];

      // setup params

      const csvFile = s3.getObject(params).createReadStream(); // works fine

      csvFile
        .pipe(csv())
        .on('data', function(entry) {
          data.push(entry);
        })
        .on('end', () => {
          console.log(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList
        });
});

I guess I need a promises or something?

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

0

you can change every item to Promise, then use Promise.all():

const getItem = (myElem) => {
  const data = [];

  // setup params

  const csvFile = s3.getObject(params).createReadStream(); // works fine

  return new Promise((resolve) => {
    csvFile
      .pipe(csv())
      .on('data', function (entry) {
        data.push(entry);
      })
      .on('end', () => {
        resolve(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList
      });
  });
};

const start = () => {
  // all promises have been finished
  return Promise.all(myCsvList.map(myElem => getItem(myElem)));
}

myCsvList.map(myElem => getItem(myElem)) can get a promise list, when all promises have been resolved, it will trigger Promise.all().

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!