Last active
March 5, 2018 20:43
-
-
Save andrIvash/f04dd1e1613a354d360c37f2cc5150db to your computer and use it in GitHub Desktop.
hw-node-2
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'); | |
| 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