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

Calling await on an unwrapped lazy query trigger does not actually wait for the request #3662

Closed
bradsherman opened this issue Aug 18, 2023 · 6 comments
Milestone

Comments

@bradsherman
Copy link

Redux toolkit version

"@reduxjs/toolkit": "^1.9.3",

I tried looking at existing issues for a similar problem but I did not find anything. I found #3515 but that seems to be more stylistic in nature. What I'm experiencing is either a bug or I am misunderstanding the documentation.

I'm trying to manually trigger a query to the server (i.e. don't use any cached data). This seems like a great use case for useLazyQuery. It looks like I should be able to call .unwrap() after calling the trigger and receive the data or have the exception thrown once the promise is resolved or rejected. However that's not what is happening for me. Interestingly, this same pattern works fine for mutations.

I have some code like the following:

export const AuthProvider = ({ children }: { children: ReactNode; }) => {
  const [triggerUserSignin] = usePostUserSigninMutation();
  const [getUserLazy] = useLazyGetUserQuery();

  const login = async (...) => {
    try {
      const signin = await triggerUserSignin(...).unwrap();
      console.log(signin);
      const userTriggerResult = await getUserLazy().unwrap();
      console.log(userTriggerResult);
      const user = await getUser();
      console.log(user);

      ...

    } catch (err) {
      ...
    }
  };
  
};

export const getUser = (): Promise<AxiosResponse<User>> => {
  return axios({
    method: 'GET',
    url: `/user`,
    responseType: 'json',
  });
};

What I'm noticing is that userTriggerResult is undefined when I print it and user is a resolved user. useLazyGetUserQuery and getUser are hitting the same endpoint - I copied getUser to demonstrate that it's just an axios query and that works fine. For now I am getting around this issue by using a raw axios query but I'd like to keep everything uniform. And like I said the mutation works like a charm - in the above snippet the console correctly prints out the signin response.

Am I misunderstanding or mis-using something? If I can provide any more information please just let me know what to provide.

Thanks in advance!

@EskiMojo14
Copy link
Collaborator

if you don't call unwrap, what does the promise return?

@rwilliams3088
Copy link

+1. I think I am seeing the same problem: #3706

@markerikson
Copy link
Collaborator

Can you try the latest CodeSandbox CSB build from #3089 and see if this is resolved?

@markerikson
Copy link
Collaborator

I'm going to assume that this is fixed by #3709 , and that fix is available in https://github.com/reduxjs/redux-toolkit/releases/tag/v2.0.0-beta.4 . Please let us know if there's further issues!

@blake-logik
Copy link

Has anyone confirmed that this is resolved? I think I'm still seeing it in 2.2.6.

@markerikson
Copy link
Collaborator

markerikson commented Oct 8, 2024

We had another related issue filed in #4650 and found what we think was the actual bug. This should now be fixed as of #4651 and is out in https://github.com/reduxjs/redux-toolkit/releases/tag/v2.2.8

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

5 participants