JasonWang34
JasonWang34

Reputation: 35

fetch().then / fetch different?

I am using fetch to check if my images exist then return true/false to replace the original image.

However, it kind of confusing me between fetch and fetch.then, when I only put url into fetch() to check its status, this works fine for me by using if condition, then I am trying to use fetch(url).then(response => ...) to do the same thing, it outputs different result.

What I expect is these two ways should output the same result, is there any blind spot?

  $('.src-load .rwd-1440').each(function(index, ele){
      var srcset =  $(ele).attr("srcset");                
         if(!urlexist(srcset)){
            var src = srcset.substring(0, srcset.indexOf('/1440')); 
            $(ele).attr("srcset", src);
         }
  }); 
    
  function urlexist(url){
      var myRequest = new Request(url);

      fetch(myRequest).then(function(response) {
          if(response.status != 404){
            return true;
          }
          else
          return false;
      });
  }
<picture class="src-load">
   <source class="rwd-1440" media="(min-width: 801px) and (max-width: 1440px)" srcset="image.png/1440.png">
   <img src="image.png">
</picture>

Upvotes: 0

Views: 24

Answers (0)

Related Questions