ppowell777
Well-Known Coder
I have no idea why this is happening, but every single time I run this simple function that fetches URL content and expect a String or Number, I get a Promise object, and I can't figure out why.
Can someone help me figure this out?
Thanks
JavaScript:
/**
* Options object based on https://stackoverflow.com/questions/41030425/disabling-cors-using-js-fetch
*/
async function checkUrl(url) {
let text = '';
try {
const controller = new AbortController();
const timeout = setTimeout(() => { controller.abort(); }, 5000);
/*const options = {
method: 'GET',
mode: 'no-cors',
signal: controller.signal
};*/
await fetch(url, { signal: controller.signal })
.then((response) => {
// BASED ON https://dmitripavlutin.com/timeout-fetch-request/ PUT clearTimeout() BEFORE THE return STATEMENT BLOCK
clearTimeout(timeout);
/*if (response.status >= 200 && response.status < 400) {
//console.log(`GOOD response code = ${response.status}`);
return true;
} else {
//console.log(`BAD response code = ${response.status}`);
return false;
}*/
return response.text();
})
.then((myText) => {
text = myText;
})
.catch((e) => { return new TypeError(`Unable to obtain response code: ${e.message}`) });
} catch (e) {
//console.error(`Unable to run catchUrl(): ${e.message}`);
return new TypeError(`Unable to run checkUrl(): ${e.message}`);
}
return text;
}
Can someone help me figure this out?
Thanks