2

I have the following JS code that uses an API to fetch some JSON objects.

function countProperties(obj) {
    var count = 0;

    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            ++count;
    }

    return count;
}
experts = {};

for(var key in data) {
  (function(){
    var k = key;
    $.post('../api/counsellor/slots/'+data[k].id,{"date":"2018-12-21","counsellor_id":data[k].id}, function(status,message){
        experts[k] = countProperties(status);
    });
  })(); 
}
console.log(experts)

But console.log(experts) displays and empty object. It displays the proper object when written inside the for loop though. What is going on here?

John Leaf
  • 63
  • 1
  • 7

0 Answers0