How can I handle asynchronous operations in JavaScript to prevent my callback from returning an empty array? Here's the code snippet:
function getPicturesArray(nbr, callback){
var avatarArr = [];
for(var i = 0; i < nbr; ++i){
request("https://randomuser.me/api/", {json: true}, (err, response, body) => {
avatarArr.push(body.results[0].picture.large);
});
}
callback(avatarArr);
}
getPicturesArray(12, (e) => {
console.log(e);
});
console.log('here');
The output is: [] here