Javascript Equivalent to PHP file_exist(filename)

This JavaScript function checks whether a file exists. However it does not check if a directory exists.

function file_exists(filename) {
    fetch(filename).then(function(resp) {
        if (resp.status == 200) {
            return true;
        } else {
            return false;
        }
    });
}

Example:

Given the HTML and Javascript:

<img id="pet" src="../images/mydog.jpg">

if (file_exists("../images/mycat.jpg")) {
    document.getElementById("pet").src = ."./images/mydog.jpg";
}