Load Images Asynchronously


(Last modified: )
/**
 * Asynchronously loads an image from the given source URL.
 *
 * @param {string} src - The source URL of the image to load.
 * @returns {Promise<HTMLImageElement>} A promise that resolves with the loaded image element,
 * or rejects with an error if the image fails to load.
 */
async function loadImage (src) {
    return new Promise((resolve, reject) => {
        const img = new Image()

        img.onload = () => {
            resolve(img)
        }

        img.onerror = (err) => {
            reject(new Error(`Failed to load image: ${src}. Error: ${err}`))
        }

        img.src = src
    })
}