Created
September 23, 2020 06:35
-
-
Save gauravkrp/c1e8eafdd0b4f2c29cc92aed440d23eb to your computer and use it in GitHub Desktop.
JS - Check if image exists
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
export default (imgPath: string) => { | |
return new Promise(resolve => { | |
const img = new Image(); | |
img.src = imgPath; | |
img.onload = () => resolve(true); | |
img.onerror = () => resolve(false); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
import checkIfImageExists from './checkIfImageExists ';
let imgSrc = "http://domain.com/your-image.jpg";
console.log(checkIfImageExists (imgSrc));