Checking If A File Exists

Since @senocular picks on me for using xhr instead of Fetch, I figured Iā€™d post a solution that uses the newer Fetch API instead :stuck_out_tongue:

fetch("https://www.kirupa.com/book/images/learning_react2.gif", {
    method: "head",
    mode: "no-cors"
})
.then(function(response) {
    if (response.status == 200) {
        console.log("file exists!");
    } else {
        console.log("file doesn't exist!");
    }
    return response.text();
})
.catch(function(error) {
  console.log("Error ", error);
});

The result should be nearly identical!

Cheers,
Kirupa