[
{
image: 'base64...'
},
{
image: 'base64...'
}
]
const arr = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg'];
const arrImg = [];
const getImg = url => {
return fetch(url)
.then(response => {
return response.blob();
})
.then(blob => {
const reader = new FileReader();
reader.addEventListener('loadend', function() {
arrImg.push({image: reader.reuslt});
});
reader.readAsDataURL(blob);
});
};
document.querySelector('button').addEventListener('click', () => {
arr.forEach(item => {
getImg(`./images/${item}`);
});
console.log(arrImg);
});
const getImg = url => fetch(url)
.then(r => r.blob())
.then(blob => new Promise(resolve => {
const reader = new FileReader();
reader.addEventListener('loadend', () => resolve({ image: reader.result }));
reader.readAsDataURL(blob);
}));
document.querySelector('button').addEventListener('click', async () => {
const arrImg = await Promise.all(arr.map(n => getImg(`./images/${n}`)));
console.log(arrImg);
});
const arr = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg'];
const getImg = async (url) => {
const response = await fetch(url);
const blob = await response.blob();
const reader = new FileReader();
return new Promise(resolve => {
reader.addEventListener('loadend', function() {
resolve({image: reader.result});
});
reader.readAsDataURL(blob);
});
}
document.querySelector('button').addEventListener('click', async () => {
const arrImg = await Promise.all(arr.map(
url => getImg(url)
));
console.log(arrImg);
});