Created
February 17, 2020 10:30
-
-
Save hfossli/91514d6b4a445a1441f207c7e694fd19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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