I have an array of objects, each object has some properties, each property contains an url of an image.
I have more arrays, and I want download all these images, so I use fetch()
promise.
Promise.all()
accepts an array of promises, so I run Promise.all()
for each property, since I don't want to have an array with all the promise for all the properties.
But.. do I have to write all of them manually? I know I can't use for loops. Or do I have to use recursion (if it is possible in this case), each time calling a function from the inside with a new array?
EDIT: the use case
//first I build arrays for promises
// var results is an array of arrays that contain objects -> results[i]Items[k].property
var items_offline_img = [];
for (var i = results.length - 1; i >= 0; i--) {
for (var k = results[i].Items.length - 1; k >= 0; k--) {
var images = new Object();
images.Src = results[i].Items[k].Src;
images.Src2 = results[i].Items[k].Src2;
images.Src3 = results[i].Items[k].Src3;
items_offline_img.push(images);
}
}
// Now I have to make Promise.all() for every items_offline_img[i].Src, and then for Src2 and so on.
// Promise block
var promises = [];
for (var i = items_offline_img.length - 1; i >= 0; i--) {
promises.push(fetch(items_offline_img[i].Src));
}
Promise
.all(promises)
.then(function(response) {
var blobs = [];
for (var i = response.length - 1; i >= 0; i--) {
var blb = response[i].blob();
blobs.push(blb);
}
return Promise.all(blobs);
})
.then(function(blobsPromise) {
var urlCreator = window.URL || window.webkitURL;
for (var i = blobsPromise.length - 1; i >= 0; i--) {
lcl_images[i].value = urlCreator.createObjectURL(blobsPromise[i]);
}
setItem();
})
.catch(function(error) {
console.log(error);
});
And now have I to write manually additional Promise blocks for Src2, Src3, and so on?
Ok I could fetch all images with only 1 array, but I have to turn images in blobs and store them with localForage.
By this way, with only 1 array, in .then(function(blobsPromise)
I don't know to which item the image referts to.
Workaround: I could replicate the array structure with images only. Like
replicate_Items[i].Src
replicate_Items[i].Src2
repliacte_Items[i].Src3
where replicate_Items[i].Src
, replicate_Items[i].Src2
, replicate_Items[i].Src3
in blobsPromise[i]
, blobsPromise[i+1]
, blobsPromise[i+2]
.then(function(blobsPromise) {
var urlCreator = window.URL || window.webkitURL;
var images_num = Object.keys(replicate_Items[0]).length;
var current_image_num = 0;
var k = 0; // replicate_Items[k]
var objectURL;
for (var i = blobsPromise.length - 1; i >= 0; i--) {
objectURL = urlCreator.createObjectURL(blobsPromise[i]);
if (current_image_num == 1)
replicate_Items[k].Src = objectURL;
if (current_image_num == 2)
replicate_Items[k].Src2 = objectURL;
if (current_image_num == 3)
replicate_Items[k].Src3 = objectURL;
current_image_num++;
if (current_image_num = images_num) {
current_image_num = 1;
k++;
images_num = Object.keys(replicate_Items[k]).length;
}
}
setItem();
})
What do you think?