Estoy aprendiendo JavaScript y las funciones asíncronas no son mi fuerte.
Tengo una serie de gameIds, y tengo que buscar cada ID individual y obtener los datos de todos esos juegos.
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); }Puede alguien ayudarme con esto ?
La mejor idea para su problema es crear un Array of Promises y ejecutarlo con 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);