Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Swapping from XMLHttpRequest() to Fetch() and still get boolean output

I know there's zillions of ways of doing this ...

So I have this function which returns me boolean upon HEAD file check status using XMLHttpRequest.

const urlToFile = "shorturl.at/devIK";
function doesFileExist(urlToFile) {
  let xhr = new XMLHttpRequest();
  xhr.open('HEAD', urlToFile, false);
  xhr.send();
  return xhr.status == "404" ? false : true;
}

However, deprecated.

Now trying to build and understand how to handle data output from fetch API. Turns out I'm not able to get desired output.

const urlToFile = "shorturl.at/devIK";
function doesFileExist(urlToFile) {
  return fetch(urlToFile, { method: 'HEAD' })
   .then(res => { res.ok ? true : false; })
   .then(fileExistResult => fileExistResult.boolValue())
   .catch(err => console.log('Error: ', err));
}

-->

doesFileExist(urlToFile) == true ? do_this : do_that;

I keep getting undefined from console.log If you could help me out on this one... Thanks in advance!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!