Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda