0

Here is my code. When i execute this it returns 0 because the get request hasn't been completed. How would i get around this besides using async/await?

tried moving return inside of get request and it comes back undefined because the function no longer has a return. I'm stumped

function getDividendRate(ticker,token){

var sum = 0;
var rates = [];
var averageRate = 0;

$.get('https://api.darqube.com/data-api/fundamentals/stocks/dividends/'+ticker+'?token='+token,function(data){

var currentRate = data[data.length-1]

for (var i = 0; i < currentRate.quarterly.length; i++){
    var rateAmount = currentRate.quarterly[i].rate;
    rates.push(rateAmount)
    console.log(rateAmount)
    sum = sum + rateAmount;
    }

    averageRate = sum/rates.length;
    //averageRate = averageRate.toFixed(2)
    //console.log(averageRate)
    })
    return averageRate;
 }
  • You will have to return a Promise from your outer function that resolves in the callback to the $.get. Although $.get probably returns a Promise anyway. – Jared Smith Dec 02 '22 at 22:20

0 Answers0