Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Last active March 5, 2018 20:43
Show Gist options
  • Select an option

  • Save andrIvash/f04dd1e1613a354d360c37f2cc5150db to your computer and use it in GitHub Desktop.

Select an option

Save andrIvash/f04dd1e1613a354d360c37f2cc5150db to your computer and use it in GitHub Desktop.
hw-node-2
const http = require('http');
var INTERVAL = process.argv[2] || 1000;
var TIME = process.argv[3] || 10000;
let clientId = 0;
async function consoleDateAndTime (id) {
let clock = setInterval(() => {
console.log(`Client: ${id}, Clock: `, new Date());
}, INTERVAL);
let result = await stopClock(clock);
return result;
}
function stopClock (clock) {
return new Promise(resolve => {
setTimeout(() => {
clearInterval(clock);
resolve(new Date());
}, TIME);
});
}
async function serverHandler (request, response) {
if (request.method === 'GET' && request.url !== '/favicon.ico') {
try {
consoleDateAndTime(++clientId).then((result) => {
response.end('Current Date: ' + result);
});
} catch (error) {
console.error(error);
}
}
}
http.createServer(serverHandler).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment