uri-planetarium / interactive-planetarium-ERAU

0 stars 0 forks source link

Using "await" with ".then" might be redundant #17

Closed Shinkyuuu closed 2 years ago

Shinkyuuu commented 2 years ago

https://github.com/uri-planetarium/interactive-planetarium-ERAU/blob/a2e557b38e2e0513f49a3f2f9d210dbcc38ad87b/player_client/src/components/joinGame/join_game.js#L59-L64

I think that having await in front of an asynchronous function call eliminates the need of using .then() afterwards. If such is the case, remove the .then() as it makes increases readability

Be sure fix every instance of this issue

Shinkyuuu commented 2 years ago

I might take this back

Shinkyuuu commented 2 years ago

This is true, using await is indeed another way of saying .then(). The difference is that using await will pause the whole function execution until the promise settles. Using .then() will let the function continue but won't call the code within .then() until the promise settles.

I believe our current implementation is equivalent to calling await twice.

const response = await fetch(`/api/lobbys/${game_code}/${player_id}`)
        .then(response => response.json());

The await pauses our function execution until the awaited function (fetch) is complete. However, adding .then() to the end of it also has the function await for its completion as well I think

Resource: https://dev.to/masteringjs/using-then-vs-async-await-in-javascript-2pma