Skip to content

Instantly share code, notes, and snippets.

@gauravkrp
Created September 23, 2020 06:35
Show Gist options
  • Save gauravkrp/c1e8eafdd0b4f2c29cc92aed440d23eb to your computer and use it in GitHub Desktop.
Save gauravkrp/c1e8eafdd0b4f2c29cc92aed440d23eb to your computer and use it in GitHub Desktop.
JS - Check if image exists
export default (imgPath: string) => {
return new Promise(resolve => {
const img = new Image();
img.src = imgPath;
img.onload = () => resolve(true);
img.onerror = () => resolve(false);
});
};
@gauravkrp
Copy link
Author

How to use:

import checkIfImageExists from './checkIfImageExists ';

let imgSrc = "http://domain.com/your-image.jpg";

console.log(checkIfImageExists (imgSrc));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment