Angus-Leung / hideaway-leaderboard

2 stars 1 forks source link

[Enhancement] Use async/await #10

Open awaisabir opened 1 year ago

awaisabir commented 1 year ago

https://github.com/Angus-Leung/hideaway-leaderboard/blob/9dde280604a3918a88f31395042fb9236c517b1f/dashboard/src/components/leaderboard/Leaderboard.jsx#L68

Can we look into using async/await? It provides a syntactically synchronous way of looking at promises

Before:

 fetch("http://18.191.238.238/players") // http://18.191.238.238/players  http://127.0.0.1:8000/players
      .then((res) => res.json())
      .then((data) => {
        setPosts(data);
      })
      .catch((err) => {
        console.log(err.message);
      });

After:

try {
  const result = await fetch("http://18.191.238.238/players");
  const data = await result.json();
  setPosts(data);
} catch (err) {
  // do something with error
  console.log(err.message);
}

Not necessary, but feels good, and not feels bad.

branyip commented 1 year ago

@awaisabir agreed, great suggestion.