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

169
Visualizações
Async function with multiple fetch in an array - Best way to do that?

I'm learning JavaScript and async functions is not my strength.

I have an array of gameIds, and I have to fetch each individual id and get the data of all that games.

async function gameInfo() {
    gamesIds = [19,12,13,15,16];

    //what should be here?

    const response = await fetch(`http://localhost:8080/game/${individualGameId}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        withCredentials: true,
        credentials: 'include',
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5c"
      },
    });

    const allGamesInfo = await response.json();
    console.log(allGamesInfo);
}

Can someone help me with this ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The best idea for your problem is creating a Array of Promises and execute it with Promise.all

gamesIds = [19,12,13,15,16];

    const promises = gamesIds.map(id =>                 
      fetch(`http://localhost:8080/game/${id}`, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          withCredentials: true,
          credentials: 'include',
          "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5c"
        },
    }))

    const response = await Promise.all(promises);

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