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

168
Views
Many requests at the same time, maybe needs Promise.all

I have a problem, which I try to solve but I'm a bit lost but I think it's simple and you could help me.

I have a function that obtains data, each data can take time to receive it, or even nothing is received, that's why I include several loops in the function that allow me to retry a maximum of attempts and get data of a max number of elements.

// get from zero to max number of elements
export const getFromZero = async (uri: string, max: number) => {
  log(`Fetching ${chalk.white.bold(max.toString())} tokens`, "info");
  let data = [];
  let i = 0;

  while (i <= max) {
    // Find and Retry
    let re = await get(uri, i, max);
    if (re === null) {
      for (let j = 0; j < 20; j++) {
        re = await get(uri, i, max);
        if (re !== null) {
          break;
        }
      }
      if (re === null) {
        log(`Element ${chalk.white.bold(i.toString())} not found`, "error");
      }
    }
    if (re) {
      data.push(re);
    }
    i++;
  }
  log(
    `Obtained ${chalk.white.bold(data.length.toString())} elements`,
    "success"
  );
  return data;
};

The problem here is that I search for the data 1 by one using the await function before moving on to the next search, which causes the process to be slow by having to receive, for example, 5000 elements, what I want to do is that the searches be done all at the same time, or wait a minimum amount of MS between each request, but I want to return the data at the end of the function once all the requests have been received or the retries of each one have been exhausted, I need to obtain all the data without having to wait between 1 call and another but only return it at the end of the process.

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!