Last active
August 28, 2019 10:00
-
-
Save peschee/1dfec2c187e7c676abc51f84839641da to your computer and use it in GitHub Desktop.
This file contains hidden or 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 http = require('http') | |
// function returns a Promise | |
function httpGet (url) { | |
return new Promise((resolve, reject) => { | |
http.get(url, (response) => { | |
const chunks = [] | |
response.on('data', (fragments) => { | |
chunks.push(fragments) | |
}) | |
response.on('end', () => { | |
const responseBody = Buffer.concat(chunks) | |
resolve(responseBody.toString()) | |
}) | |
response.on('error', (error) => { | |
reject(error) | |
}) | |
}) | |
}) | |
} | |
async function makeSyncGetRequest (url) { | |
try { | |
const response = await httpGet(url) | |
console.log(response) | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
const duration = 5 | |
let count = 0 | |
const interval = setInterval(() => { | |
if (count++ >= duration) { | |
clearInterval(interval) | |
return | |
} | |
makeSyncGetRequest('http://www.google.com') | |
}, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment