mutateAsync vs mutate with options. What's the difference?
The docs on this page: https://tanstack.com/query/latest/docs/framework/react/guides/mutations#promises say "Use mutateAsync instead of mutate to get a promise which will resolve on success or throw on an error. This can for example be used to compose side effects." with the following code as an example:
But given that you can pass options to the mutate function, wont this code produce the same results?:
Is there any meaningful difference? Thanks!
4 Replies
optimistic-gold•2y ago
Yeah, in this example it is the same
But mutateAsync returns the result as a promise which you could further use in the scope whereas mutate only provides the result in a callback
equal-jade•2y ago
It seems to matter when doing consecutive mutations.
stormy-gold•2y ago
Another use-case keeping in mind that it returns a promise: show state messages using toasts (loading, success or error)
Ref: https://sonner.emilkowal.ski/toast#promise
Toast() – Sonner
Nextra: the next docs builder
stormy-gold•17mo ago
we do that at work
:thumbs_Up: