Skip to content

Instantly share code, notes, and snippets.

@iamnasirudeen
Created March 14, 2021 00:22
Show Gist options
  • Save iamnasirudeen/f3b280eb2f106465ae71dea64c21d440 to your computer and use it in GitHub Desktop.
Save iamnasirudeen/f3b280eb2f106465ae71dea64c21d440 to your computer and use it in GitHub Desktop.
A simple explanation on how eventLoop works
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