Skip to content

Instantly share code, notes, and snippets.

@hfossli
Created February 17, 2020 10:30
Show Gist options
  • Save hfossli/91514d6b4a445a1441f207c7e694fd19 to your computer and use it in GitHub Desktop.
Save hfossli/91514d6b4a445a1441f207c7e694fd19 to your computer and use it in GitHub Desktop.
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