T
TanStack4y ago
rare-sapphire

data from multiple endpoints

I built data from two different endpoint with one single queryKey. Is there a better pattern? Thanks
const fetch= async () => {
const { data1, error } = await axios ....
const { data2, error: err } = await await axios ....
if (error) {
throw new Error(error.message)
}
if (err) {
throw new Error(err.message)
}
return { ...data1, ...data2 }
}

export function useProject() {
return useQuery([cacheKey, projectId], () => fetch())
}
const fetch= async () => {
const { data1, error } = await axios ....
const { data2, error: err } = await await axios ....
if (error) {
throw new Error(error.message)
}
if (err) {
throw new Error(err.message)
}
return { ...data1, ...data2 }
}

export function useProject() {
return useQuery([cacheKey, projectId], () => fetch())
}
2 Replies
harsh-harlequin
harsh-harlequin4y ago
You can use Promise.all method to do aggregation of promise state for you
rare-sapphire
rare-sapphireOP4y ago
thx.

Did you find this page helpful?