Am I required to hold ref to HTMLImageElement
to prevent it from being gc'ed befor its load
/error
events will be fired?
for example:
/**
* @param { string[] } urls
* @returns { Promise<HTMLImageElement[]> }
*/
function loadImages(urls) {
return new Promise(function(resolve) {
/**@type { HTMLImageElement[] } */
const images = [];
let i = urls.length;
/**@this { HTMLImageElement } */
function onLoad() {
images.push(this);
if (--i === 0) resolve(images);
}
function onError() {
if (--i === 0) resolve(images);
}
for (const url of urls) {
const image = new Image();
image.src = url;
image.addEventListener("load", onLoad);
image.addEventListener("error", onError);
}
});
}
is it possible to some of images being lost due to gc or not ?
it looks like i really need to do it, but there also can be some special rule for cases like this