How To Force Downloaded Files To Stay In Same Position In Array?
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 a
Solution 1:
You can use Promises
and map the async callbacks to a Promise.all()
:
Promise.all(imageArray.map(function(image) {
returnnewPromise(function(resolve, reject) {
image.download(function(err, contents) {
if (err) {
reject(err);
} else {
resolve(base64_encode(contents));
}
});
});
})).then(function(fileArray) {
res.render("home", {keyArray: keyArray, titleArray: titleArray, usernameArray: usernameArray, imageArray: fileArray});
}).catch(function(err) {
// uh oh
});
Bonus is you don't need to check the array size to confirm completion.
Post a Comment for "How To Force Downloaded Files To Stay In Same Position In Array?"