From d0fbaedb3b44c55c2704d11d5b9d416b1baec759 Mon Sep 17 00:00:00 2001 From: SidharthK2 Date: Mon, 16 Sep 2024 23:44:57 +0530 Subject: [PATCH] remove promise.all --- src/utils/use-stats-data.ts | 96 ++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/src/utils/use-stats-data.ts b/src/utils/use-stats-data.ts index ce99a622..3bc90486 100644 --- a/src/utils/use-stats-data.ts +++ b/src/utils/use-stats-data.ts @@ -24,29 +24,89 @@ export function useStatsData() { useEffect(() => { const fetchData = async () => { - const [holders, volume, iq, lp, social, ep] = await Promise.all([ - getTokenHolders(), - getVolume(), - getIQ(), - getLPs(), - getSocialData(), - getEpData(), - ]) - - const newData = { ...holders, ...volume, ...iq, ...lp, ...social, ...ep } - - setData(newData) - - setTotals({ - holders: getMappedValue(holders.holders), - volume: getMappedValue(volume.volume), - }) + const newData: Dict = {} + + const fetchHolders = async () => { + try { + const holders = await getTokenHolders() + newData.holders = holders.holders + setData((prevData) => ({ ...prevData, ...newData })) + setTotals((prevTotals) => ({ + ...prevTotals, + holders: getMappedValue(holders.holders), + })) + } catch (error) { + console.error('Error fetching holders:', error) + } + } + + const fetchVolume = async () => { + try { + const volume = await getVolume() + newData.volume = volume.volume + setData((prevData) => ({ ...prevData, ...newData })) + setTotals((prevTotals) => ({ + ...prevTotals, + volume: getMappedValue(volume.volume), + })) + } catch (error) { + console.error('Error fetching volume:', error) + } + } + + const fetchIQ = async () => { + try { + const iq = await getIQ() + newData.Iq = iq.Iq + setData((prevData) => ({ ...prevData, ...newData })) + } catch (error) { + console.error('Error fetching IQ data:', error) + } + } + + const fetchLPs = async () => { + try { + const lp = await getLPs() + newData.lp = lp.lp + setData((prevData) => ({ ...prevData, ...newData })) + } catch (error) { + console.error('Error fetching LP data:', error) + } + } + + const fetchSocial = async () => { + try { + const social = await getSocialData() + newData.social = social.social + setData((prevData) => ({ ...prevData, ...newData })) + } catch (error) { + console.error('Error fetching social data:', error) + } + } + + const fetchEp = async () => { + try { + const ep = await getEpData() + newData.ep = ep.ep + setData((prevData) => ({ ...prevData, ...newData })) + } catch (error) { + console.error('Error fetching EP data:', error) + } + } + + fetchHolders() + fetchVolume() + fetchIQ() + fetchLPs() + fetchSocial() + fetchEp() } + if (!isFetched.current) { isFetched.current = true fetchData() } - }, [data, totals]) + }, []) return { data, totals } }