I'm trying to fetch a list of URLs in an array and then covert them all to Base64. I however need to block the function from running until all the images are fetched and processed since the output is required for the next step.
The expected process should be
let example = ["https://www.example.com/1.jpg", "https://www.example.com/2.jpg"];
converted to a Base64 encoded array.
//Contains the B64 encoded images
var observationImages = []
//List of Images to fetch and encode
var lookupPhotos = ['https://www.example.com/1.jpg', 'https://www.example.com/2.jpg'];
Promise.all(
lookupPhotos.map(url => {
fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
console.log('Converting to b64');
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
}))
.then(dataUrl => {observationImages.push(dataUrl)});
}
)
).then(data => {
console.log('Promises all resolved!');
console.log(observationImages);
});
I've tried using Promises but I'm not sure I fully understood how they work so it doesn't work as expected.