From 2c4f61d3f71525cc25cf995bcf031a89aa6edc47 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Sun, 19 Mar 2023 09:31:18 +0100 Subject: [PATCH] * Fixed race condition in player headers fetch --- .../src/components/Pong/GameComponent.svelte | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/front/volume/src/components/Pong/GameComponent.svelte b/front/volume/src/components/Pong/GameComponent.svelte index 83fff91..940dac4 100644 --- a/front/volume/src/components/Pong/GameComponent.svelte +++ b/front/volume/src/components/Pong/GameComponent.svelte @@ -29,26 +29,25 @@ $: { if (game && game.players.length > 1) { - getGameUsers(); + void getGameUsers(); } } - function getGameUsers() { + async function getGameUsers() { gameUsers = []; - game.players.forEach(player => { - fetch(API_URL + "/users/" + player.name + "/byname", { + for (const player of game.players) { + console.log(player.name) + const response = await fetch(API_URL + "/users/" + player.name + "/byname", { credentials: "include", method: "GET", mode: "cors", - }).then((response) => { - if (response.ok) { - response.json().then((user) => { - gameUsers.push(user); - gameUsers = gameUsers; - }); - } - }); - }); + }) + if (response.ok) { + const user = await response.json() + gameUsers.push(user); + } + } + gameUsers = gameUsers; }