I'm trying to make a function that checks if the given URL is working or not. This is what I've done so far:
function checkWebsite(url) {
http
.get(url, function(res) {
console.log(url, res.statusCode);
return res.statusCode === 200;
})
.on("error", function(e) {
return false;
});
}
I want to wait until http get is finished and then return true or false based on the status code and being the response in a callback function I don't know how to return it back to where it was called. Example call:
function test() {
if (checkWebsite("https://stackoverflow.com/")) {
return "https://stackoverflow.com/";
}
}
I want to get the true or false in this if statement but since the status code is in a callback function I don't know how to return it.