Reputation:
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 ?
Upvotes: 0
Views: 1515
Reputation: 71
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);
Upvotes: 3