I'm trying to calculate a sum of results from an external API and I need to make a single request for each keyword I have. The code executes successfully but the function returns the value before all the ajax requests are completed. I tried to add "async" param but it's deprecated then I made this:
function calcTotal(arr) {
var t_docs = 0;
var i = 0;
var len = arr.length;
var aReq = []
for(i = 0; i < len; i++)
{
    console.log(i);
    var apiDataLQKT = {
        keyword: arr[i],
        start_date: "2015-01-01",
        end_date: "2015-06-01",
        format: 'jsonp',
        sources_types: sources,
        sources_names: names,
        domains: argDomains,
        words: terms,
        author_id: usrID,
        sentiment:sentiment,
        positive_threshold:posThresh,
        negative_threshold:negThresh,
        language:lang,
        author_location:geolocations,
        author_gender:genderID,
        typologies:typID,
        document_type:docType,
        source_base_url:'',
        emotion:'',
        metadata:'',
        order_sort:'',
        order_by:''
    }
    console.log(apiDataLQKT);
    aReq[i] = $.ajax({
        type:'GET',
        url:apiUrl+'LightQuantitativeKeywordTrend',
        contentType: 'application/javascript',
        crossDomain: true,
        dataType: 'jsonp',
        data: apiDataLQKT,
        success: function(json) {
            var res = json.LightQuantitativeKeywordTrend;
            t_docs += res.count;
            console.log("T_DOCS[" + arr[i] + "]: " + t_docs);
        }
    });
}
aReq[arr.length-1].done(function(data){
    return t_docs;
});
}
console log outputs:
Total: 0
T_DOCS[undefined]: 1445
T_DOCS[undefined]: 1521
...
What else can I try?
 
    