I have a script that fetch stocks symbols from the data base and doing API calls to get stock price. After 30 iteration, the script failed, it seems like there is a rate limit on the Stocks API server so I want to add dealy between the iterations in order to find the best frequency. This is my script:
const apiQueue = [];
const newPrices = {};
table.items.forEach(stock =>
{
// console.log(stock.symbol)
apiQueue.push(getJSON(console.log(`https://financialmodelingprep.com/api/v3/stock/real-time-price/AAPL`)).then(json =>
{
newPrices[json.symbol] = json.price;
}));
});
await Promise.all(apiQueue);
table.items.forEach(stock =>
{
stock.change = (newPrices[stock.symbol] - stock.price) / stock.price * 100;
stock.price = newPrices[stock.symbol];
});
I'm new in JavaScript, I tried everything, and I can't make it work. My limit right now is 30 API calls, can I divide the foreach loop and send 30 calls every few seconds?.
Update
Thank you guys, i tried what you told me to do but i can't get it work. that's my last test:
fetch('https://financialmodelingprep.com/api/v3/stock/real-time-price/aapl,wpm,bhf')
.then(response => {
return response.json()
})
.then(data => {
// Work with JSON data here
var myJSON = JSON.stringify(data);
console.log(myJSON)
for(var i = 0; i < myJSON.length; i++) {
var obj = myJSON[i];
console.log(obj.symbol);
}
})
I tried all the examples on this post JavaScript loop through json array? non of them work.