Hello Guys,

We live in the busy world, where nobody wants to wait for anything. I guess this is the primary reason for the growing popularity of NodeJs. So today lets discuss must known things before you try your hand with Node.Js.

Event Driven Programming

Event Driven Programming is the style where the execution of flow depends upon the occurrence of events. So there would be the event callback function that will be invoked when any significant event occurs, Such as result returns from a database query.

Let’s take an example, as how the database query handles in the typical blocking I/O programming.

resultData = query(‘select * from user where id=1’); // This query will stop all the process or current thread until the data returns
Let’s see how this would work in the Event Driven System.

proceed: function(resultData) {
createData(resultData) // step 5


query(‘SELECT * FROM posts WHERE id = 1’, proceed); // Step1

do some other stuff…… Step2
……. Step3
….. Step4

So in Event Driven Programming, the process doesn’t halt the flow of execution and continue his execution whenever there is async operation come in the way and proceed to complete his current stack. And even if the result from async calls comes it would wait for the current stack to finish and then call the what next in his stack.


If you would be asked me one reason of Success of Node. I would say, it’s because of Closures. So what are closures?
As we know, in Javascript, functions are the first class functions. That means that function can be passed as the argument(Call back) in other function.

So when the callback function would be called, it magically remembers the context in which it was declared, along with all the variables available in that context and any Parent Context. Let’s take an example.

click count is 1
click count is 2
click count is 3

See, we have started with called function A, which eventually create function B (not calling it), and then function B being pass to the function C as an argument. And inside function C, argument function(ie function B) is calling thrice. So the point to be noted that variable clickCount defined in the function A, is still accessible in the function C. This is primarily due to the Closures. Once again, closures magically remembers the context in which it declared, and can be accessed the time you called that.

So hope you enjoyed this. Happy Living.:)

“Victorious warriors win first and then go to war, while defeated warriors go to war first and then seek to win”