Reputation: 43
I'm making an app that fetches data from 2 different APIs at the same time based on the user's search.
The app is supposed to fetch current crypto prices and historical data. But the APIs use 2 different formats for the symbol, like so:
1.
if someone looks up 'BITCOIN' it's only going to pull up data from 1 API, because the other 1 requires 'BTC-USD'
2.
Is it possible to somehow fetch data from both APIs at the same time-based?
I guess I could see some solutions (hardcoding the data, or switching APIs so they use the same format), altho this path seems pretty work-intensive.
Any ideas on how this can be done?
Upvotes: 0
Views: 438
Reputation: 819
Use Promise.all
if you want fetch data from 2 different API's at the sane time:
const fetchData = async (yahoo_API_URL, coingecko_API_URL) => {
const fetchYahoo = fetch(yahoo_API_URL, /* config */)
const fetchCoingecko = fetch(coingecko_API_URL, /* config */)
const [fetchYahooRes, fetchCoingeckoRes] = await Promise.all([fetchYahoo, fetchCoingecko])
const [cryptoPriceFromYahoo, cryptoPriceFromCoingecko] = await Promise.all([fetchYahooRes.json(), fetchCoingeckoRes.json()])
...
}
fetchData('yahoo.com/BTC-USD', 'coingecko.com/api/BITCOIN')
fetchData('yahoo.com/ETH-USD', 'coingecko.com/api/ETHEREUM')
Upvotes: 1