Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const https = require("https");
function fetchStatusCode(url) {
return new Promise(function(resolve, reject) {
const req = https.request(url, res => {
resolve(res.statusCode);
});
req.on("error", error => {
reject(error);
});
req.end();
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function testConnection() {
while (true) {
const startTime = new Date().getTime();
try {
const statusCode = await fetchStatusCode("https://google.com");
const endTime = new Date().getTime();
console.log(
`${new Date()}: Got status code ${statusCode} after ${endTime -
startTime} milliseconds when fetching google.com`
);
} catch (e) {
const endTime = new Date().getTime();
console.log("=================== ERROR ===================");
console.log(e);
console.log(
`${new Date()}: Error after ${endTime -
startTime} milliseconds when fetching google.com`
);
}
await sleep(2000);
}
}
testConnection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.