Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async and await #116

Open
jackherizsmith opened this issue Dec 6, 2022 · 0 comments
Open

async and await #116

jackherizsmith opened this issue Dec 6, 2022 · 0 comments

Comments

@jackherizsmith
Copy link

jackherizsmith commented Dec 6, 2022

You can replace .then with await and put a resolved promise in a variable instead. there's a catch with useEffect which is they don't like async functions so you can wrap it. here's what that looks like (without comments), from home-page.js:

  useEffect(() => {
    if (user) {
      (async () => {
        const userFromDb = await getUserById(user.uid);
        let username = userFromDb.username;
        setName(username);
        let dueDate = userFromDb.dueDate;
        let weekToQuery = whichWeekToQueryFromBabySize(dueDate);
        setWeekNum(weekToQuery);
        const babySizeData = await getBabySizeByWeek(`Week ${weekToQuery}`);
        setBabySizeObj(babySizeData);
        let trimesterThisUserIsIn = whichTrimesterAreYouIn(dueDate);
        router.push(`/home-page?trimester=${trimesterThisUserIsIn}`);
      })();
    }
  }, [user]);

Also whilst we're here, I would recommend using a more specific dependency for this effect, i.e. user.uid. Since you want to limit how often it runs as a main page. That being said, there aren't currently many changes to user so it's not a huge deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant