[...selectedImages].map((image, index) => {
const reader = new FileReader()
reader.readAsDataURL(image)
//тут остановить до тех пор пока reader.result === null
return (
<img src={reader.result} alt="" key={`previewImage:${index}`} />
)
})
Пишу на React
const [imageUrls, setImageUrls] = useState([]);
async function readAsDataURL(image) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(image);
});
}
useEffect(() => {
(async () => {
try {
const urlPromises = selectedImages.map(readAsDataURL);
const urls = await Promise.all(urlPromises);
setImageUrls(urls);
} catch (error) {
console.error(error);
}
})();
}, [selectedImages]);
return imageUrls.map((url, index) => <img src={url} alt="" key={`previewImage:${index}`} />);
const readAsDataURL = (image) => {
const fr= new FileReader();
return new Promise((resolve, reject) => {
fr.onerror = () => { fr.abort(); reject(new Error( 'err msg' )); };
fr.onload = () => { resolve(fr.result); };
fr.readAsDataURL(image)
});
};
[...selectedImages].map( async (image, index) => {
const data = await readAsDataURL(image)
return (<img src={data} alt="" key={`previewImage:${index}`} />
})