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

160
Views
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 answers
Answer question

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 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!