Check if image exists in typescript

I have a Google map implementation where I use tiles for maps that are on the Internet. But if I go beyond the area of ​​the tile, it throws errors to me because it cannot find these images. How to make the if statement return null when they don't exist?

What I thought about this because I include jquery:

$.get(url) .done(function() { // exists code return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png` }).fail(function() { // not exists code return null; }) 

but this does not work for me, this is the constructor of my component:

  constructor(){ this.imageMapOptions = { getTileUrl: (coord: ImageMapTypeCoord, zoom: number) => { return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png` }, tileSize: { height: 256, width: 256 }, maxZoom: 18, minZoom: 16, radius: 1738000, name: 'Beeksebergen' }; } 
+5
source share
1 answer

Something like this might work:

 var url = 'https://example.com/image.png'; return new Promise((resolve, reject) => { return $('<img>') .on('error', reject.bind(null, url)) .on("load", resolve.bind(null, url)) .appendTo(document.body) .css({ "position": "absolute", top: -9999, left: -9999 }) .on("error load", $.fn.remove) .attr("src", url); }); 
+2
source

All Articles