Skip to content

Correctly handle asynchronicity #81

Description

@stefnotch

This has a bug, can you spot it?

async function setUserStore(newUser: User | null) {
    console.log("Updating user state", newUser);
    user.value = newUser;
    if (newUser) {
      const fetchedProfile = await useProfileService().fetchProfile(newUser);
      setProfileStore(fetchedProfile);
    } else {
      setProfileStore({});
    }
  }

Bug:
When the user changes very quickly, and supabase's response times are slow, then this can happen:

  • User changes to Jane
  • We start fetching the profile (!!!)
  • Jane logs out, thus the user changes to null
  • We set the profile to empty
  • Supabase returns the profile that we tried to fetch a while ago...and now we have a logged out user, with the profile of Jane

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions