schmitty890 / sports-tracker

MIT License
1 stars 0 forks source link

async/await in for loop in doTeamsHaveGamesToday function #54

Closed schmitty890 closed 4 years ago

schmitty890 commented 4 years ago

We should not be using await inside of a for loop in the doTeamsHaveGamesToday function in the controllers/subscribe.js file. We have disabled the eslint rule around this issue as it is still working for what we need it to do, but there is a better way to code this.

We send the teamID of our subscribed team to the nhl api to see if the team is playing or not, we get a Boolean back. This async/await is not being used correctly, however works for its current purpose.


        /**
         * doTeamsHaveGamesToday takes users subscribed nhl teams and fetches if they have a game today or not
         *
         */
        const doTeamsHaveGamesToday = async () => {
          const subscribers = hbsObject.user.subscribed;

          // TODO: we can remove this await outside of the for loop but it works for the purpose of updating the ture/false if a team is playing a game, eslint is throwing error and we should fix LINK: https://eslint.org/docs/rules/no-await-in-loop
          /* eslint-disable */
          for (let i = 0; i < subscribers.length; i++) {
            const gameToday = await nhlAPI.gameToday(subscribers[i].body.teamID);
            subscribers[i].body.gameToday = gameToday;
          }
          /* eslint-enable */

          renderDashboard();
        };
schmitty890 commented 4 years ago

@Habernet , i know we talked about this tonight, feel free to push up an async/await solution, i'd love to see one. i found a solution with promise.all that seemed to work to get the await out of the for loop

closing this for now as its fixed, but feel free to push up an asyc await solution if its better or a different variation or what not