Last active
May 28, 2024 18:27
-
-
Save BroFox86/42481c5104125a927cb9688a0aa5bc11 to your computer and use it in GitHub Desktop.
[Load image with Promise] #jsTrick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadImage(src) { | |
return new Promise((resolve, reject) => { | |
const image = document.createElement('img') | |
image.src = src | |
image.onload = () => resolve(image) | |
image.onerror = () => reject(new Error(`Load image error: ${src}`)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment