I have been trying to call multiple ajax call in async mode and then waiting for all the ajax calls to complete before proceeding. I am using jquery .when().
var results_array = [];
var num = 0;
var promises = [];
ldap_cmd_array.forEach(element => {
var myldap = ldap_data;
myldap.push({
"name": "cmd",
"value": element
});
console.log(++num);
promises.push(ajaxCall(myldap, 'aaa',
// success callback
function (data) {
console.log(--num);
results_array.push(data);
console.log('pass');
},
//error callback
function (err) {
//Do nothing
console.log(--num);
console.log('fail');
}
));
});
$.when.apply($, promises)
.then(function() {
console.log(results_array);
});
But in the output, I see that results_array is printing before all the ajax call is completed. I'm not sure where I am going wrong. Need help? Thanks in advance.
Note: output image is attached.