TanStackT
TanStack3y ago
6 replies
wet-aqua

Chain useMutation together, make them rely on one another

Hi all, real quick react-query question (maybe not so quick!)

I'm a bit stumped. Inside of our application we do a bit of promise chaining. This serves two purposes
1: Logically, the promise must complete before another one gets sent, this is just how the application works
2: The execution of the promise relies on data from the response of the previous promise. For example, we send an order (the DB generates an order number and sends it in the response) and then we use said order number to put on the customers receipt.

I'd like to replace the promise chaining with react-query mutations for a few reasons, its a cleaner experience, it will give us a bit more control over each step in the process (showing the user what step in the process their order submission is at or what have you using loading states) and also allow us to retry queries that fail until they succeed, which is not something easily done with promise chaining unless we wanted 5 million lines of duplicated code.

i thought about putting the whole process inside of a usemutation, but this kinda defeats the point of the granularity of them all being seperate as we still wouldn't really get control over each step in the process

what is the best way to go about this? struggling to wrap my head around it. thanks!
Was this page helpful?