Created
March 14, 2021 00:22
-
-
Save iamnasirudeen/f3b280eb2f106465ae71dea64c21d440 to your computer and use it in GitHub Desktop.
A simple explanation on how eventLoop works
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 EventEmitter = require("events"); | |
function net() { | |
this.emitEvent = this.emitEvent.bind(this); | |
this.onConnection = this.onConnection.bind(this); | |
} | |
net.prototype = EventEmitter.prototype; | |
net.prototype.createServer = function(cb) { | |
if (typeof cb === "function") process.nextTick(cb); | |
return this; | |
}; | |
net.prototype.emitEvent = function() { | |
this.emit("listening", { port: this.port }); | |
}; | |
net.prototype.onConnection = function() { | |
this.emit("connection"); | |
}; | |
net.prototype.listen = function(port) { | |
this.port = port; | |
process.nextTick(this.onConnection); | |
process.nextTick(this.emitEvent); | |
return this; | |
}; | |
const netServer = new net(); | |
netServer.on("connection", (conn) => { | |
console.log("server connected successfuly"); | |
}); | |
netServer.createServer(); | |
netServer.listen(8080); | |
netServer.on("listening", ({ port }) => console.log("Listening on port %s", port)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment