0

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.

pythonist
  • 3
  • 2
  • Could you not just wrap your call into setTimeout? - https://www.w3schools.com/jsref/met_win_settimeout.asp – Inch High Dec 13 '19 at 12:54
  • 2
    Instead of doing 30 calls, why don't you get [everything](https://financialmodelingprep.com/api/v3/stock/real-time-price/) and filter with what you want? That would just take 1 call. – FF- Dec 13 '19 at 12:54
  • @FF in addition, it's possible the API has a filtering option already, so you can just request all items matching criteria (e.g., by ID or whatever that value is). Not sure if this API has it but it's a possibility in general. – VLAZ Dec 13 '19 at 13:28
  • 1
    @VLAZ in fact, after reading the documentation on that API, technically it doesn't expose the method for multiple values, but it does handle it OK. I just tried it like [this](https://financialmodelingprep.com/api/v3/stock/real-time-price/CYCN,DWSH,MARA), which goes to show how OP didn't even tried to read the [documentation](https://financialmodelingprep.com/developer/docs) in an extensive manner. – FF- Dec 13 '19 at 13:37
  • @FF- so a classic [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) then. All that's really needed is to get all the IDs of the elements, make them comma separated and append them to the URL. Then you make one request. – VLAZ Dec 13 '19 at 13:44

0 Answers0