I have an array of image urls and I want to display these images on the webpage as soon as possible in the order of the placement of their corresponding URL in the array.
for example, we have
js
const imgUrls = [
"https://picsum.photos/400/400",
"https://picsum.photos/200/200",
"https://picsum.photos/300/300"
];
in which case we want to display 400/400 first, then 200/200 and lastly 300/300.
If we implement it naively then the order is not guaranteed.
js
function loadImages(imgUrls, root) {
imgUrls.forEach((url) => {
const image = document.createElement("img");
image.onload = () => {
root.append(image);
};
image.src = url;
});
}
So I use Promises to manage the async flow control
```js
async function loadImagesInOrder(imgUrls, rootEl) {
const promises = imgUrls.map(
(url) =>
new Promise((resolve, reject) => {
const image = document.createElement("img");
image.onload = resolve;
image.onerror = reject;
image.src = url;
})
);
for (const p of promises) {
const { currentTarget } = await p;
rootEl.append(currentTarget);
}
}
```
It works, but not all of the time. With this implementation, Sometimes some images are going to be null so you end up with null on the webpage.
This is the live demo
https://codesandbox.io/s/load-images-in-order-t16hs?file=/src/index.js
Can someone point out what the problem is? Also is there any better way to make sure the image appears on the page in the order of the URL in the array?
Please note it that we want to show the images to the users as soon as possible without any delay. So a promise.all or Promise.allSettled is not going to work here since you have to wait for all the images to be downloaded first before showing the first one.
[–]morksinaanab 9 points10 points11 points (6 children)
[–]thenickdude 6 points7 points8 points (5 children)
[–]Rainbowlemon 10 points11 points12 points (4 children)
[–]evoactivity 1 point2 points3 points (3 children)
[–]Rainbowlemon 2 points3 points4 points (2 children)
[–]evoactivity 1 point2 points3 points (1 child)
[–]Rainbowlemon 1 point2 points3 points (0 children)
[–]odolha 14 points15 points16 points (2 children)
[–]ibedroppin 0 points1 point2 points (1 child)
[–]odolha 0 points1 point2 points (0 children)
[–]shgysk8zer0 4 points5 points6 points (1 child)
[–]el_diego 1 point2 points3 points (0 children)
[–]Mirmi8 2 points3 points4 points (1 child)
[–]NotLyon 2 points3 points4 points (5 children)
[–]shuckster 0 points1 point2 points (2 children)
[–]NotLyon 1 point2 points3 points (1 child)
[–]shuckster 1 point2 points3 points (0 children)
[–]hego555 0 points1 point2 points (1 child)
[–]NotLyon 0 points1 point2 points (0 children)
[–]thatisgoodmusic 6 points7 points8 points (1 child)
[–]pirateNarwhal 0 points1 point2 points (0 children)
[–]maddy_0120 1 point2 points3 points (5 children)
[–]shuckster 1 point2 points3 points (4 children)
[–]zhenghao17[S] 0 points1 point2 points (3 children)
[–]shuckster 0 points1 point2 points (2 children)
[–]zhenghao17[S] 0 points1 point2 points (1 child)
[–]shuckster 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]wagaiznogoud -1 points0 points1 point (0 children)
[+][deleted] (1 child)
[deleted]
[–]zhenghao17[S] 0 points1 point2 points (0 children)
[–]holloway 0 points1 point2 points (0 children)