T
TanStack3y ago
xenial-black

How can I conditionally fail after getting a response before `select()` is called?

I'm trying to make calls to Cube.dev which sometimes returns an 200 with an object that looks like { "error": "Continue wait" } for a long running query. If this is the case I need to seamlessly retry the request until the data is available. I thought I'd be able to do this with the onSuccess callback.
onSuccess: (data) => {
if ((data as CubeLongRunningError)?.error === 'Continue wait') {
throw new Error('Long running cube query');
}

if (options.onSuccess) {
options.onSuccess(data);
}
},
onSuccess: (data) => {
if ((data as CubeLongRunningError)?.error === 'Continue wait') {
throw new Error('Long running cube query');
}

if (options.onSuccess) {
options.onSuccess(data);
}
},
This kinda works, but unfortunately the above is wrapped up in a re-usable hook and I'm expecting people to also provide a select function pretty much everytime this is used. However it seems like the select is called before onSuccess (which surprised me). Is there any other way I can achieve this?
2 Replies
genetic-orange
genetic-orange3y ago
I would try putting that logic directly inside the mutation function and only return from the mutation function when it's actually done.
foreign-sapphire
foreign-sapphire3y ago
Would a refetchInteval function maybe be more fitting? That'll get passed the result of the query, where you can check for e.g. data?.error === 'Continue wait' ? number : false The selects would still run but eventually would get triggered with the real payload.

Did you find this page helpful?