Earlier today, a friend of mine wanted me to teach him how to do JavaScript sockets. Okay, I happily obliged. But honestly:
- I have only used Sockets once
- I have only built a Node socket server once, and never in another language
- Mainly I just learned sockets via Node.js videos, especially the early intro videos.
Anyways, the concept was clear, and everyone had grasp of what a public chatserver would consist. As for the code… Let’s just say I had a cheat sheet. But anyways, the session boiled down not of sockets nor the JavaScript that ran it, but instead, I was explaining more of how JavaScript ran in those lines of spaghetti.
The point is, no matter how complex your code is, your JavaScript will boil down into 2 basic paths of execution: Synchronous and Asynchronous. The former is synonymous to the concept of procedural code, whereas the latter is more akin to threading or parallel-ing. To put into simple terms, JavaScript is just a set of do’s and todo’s respectively. You either tell it what to do now, or tell it to do something ASAP.
Procedural code or “do’s” is pretty much structured like commands. It’s like telling JS “Do this, I give you this, do that, NOW!!!”. Think of it like an arsonist, and we tell him to do what he is advertised to do:
//Very harsh, isn't it?
var arsonist = new Arsonist('Frank');
arsonist.burnHouse();
arsonist.detonateMine();
arsonist.incinerateCorpse();
As for the async aspect or the “todo’s”, imagine that we are not around to tell JS what to do when things happen. The best part of async JS is that you don’t know when it happens, like AJAX for instance. You don’t know when the server responds, nor if it will respond to you at all. But instead of waiting for something that might not even happen, we give JS a very detailed “todo list” of what to do when they do happen.
So just in case someone stole our arsonist friend when we are not around and tells him what to do, imagine we set-up a fireman beforehand, and told him what to do when things happen:
//Suppose we had a fireman
var fireman = new Fireman('Pete');
//For each event, provide a "todo", which is basically a function
fireman.on('fire',function(event){
console.log('I shall put out fire');
});
fireman.on('catStuckOnTree',function(event){
console.log('I shall bring ladder');
});
fireman.on('seePrettyGirl',function(event){
console.log('Get beaten by an astronaut');
});
As an aside: Another nice thing of JS is that if programmed verbosely, it does sound like you are reading sentences instead of code. So if you omit the technical and syntactical parts, you’d be reading the bindings as “Fireman: on fire, I shall put out fire” and so on.
So there you have it. That’s another simple explanation for another complex definition in JS.





