Currently, my code loads all images async. That also means in my current implementation that the image is added to the fileArray once it has been completely downloaded. Since not all images are equal in size, downloads of each image finish at different times, resulting in the images being added to the array in a random order.
How can I force images to be added to the file array in the order their downloads start ? Hopefully without giving up the benefit of async (all images being downloaded at the same time).
for (var i = 0; i < imageArray.length; i++) {
imageArray[i].download(function(err, contents) {
fileArray.push(base64_encode(contents));
console.log("SIZE: "+fileArray.length);
if (fileArray.length == 9) {
res.render("home", {keyArray: keyArray, titleArray: titleArray, usernameArray: usernameArray, imageArray: fileArray});
}
});
}