NodeJS Series #6: Event-Driven Programming

Sharing is Caring

Hi Wizards,

Its been some time since I’ve left Hogwarts Magicians School for a small holiday.Now that Im back,im ready for some tricks.

But before that,i’d like to talk about Event-Driven Programming and in its place in NodeJS

What is Event-Driven Programming?

Event-driven programming is the term where the flow of the code is determined by events(click,load,etc…)
Its one of the basic milestone today’s popular programming languages such as C#,Java and many more I dont wanna fill up here.
In Node.js and moreover in any kind of Javascript project,you’ll be using or used event-driven process.Whether its page onload or button click event,this is something you have achieved with our without knowing.

Lets make an example to the classic event-driven process and how its done in NodeJS:

[sourcecode language=”javascript”]
result = getJSONfromDestination();

The operation above requires blocking I/O process (single thread operation that waits for previously working synchronous code )

Now lets take a look at how we do asynchronous way to do it(Non-blocking I/O process)

[sourcecode language=”javascript”]
json_finished = function(result){

As you can see,this is non blocking sample,because json_finished doesnt work first as you can imagine.
It starts working when you call getJSONfromDestination method and sending param as function to json_finished.

This is how Asynchronous operations are completed in mainly Javascript projects and widely used in NodeJS projects.In case of working step-by-step,we send params to functions to start working whenever & however we want.

You’ll be using frequently these kind of Asynchronous operations or in other terms “Event-Driven Programming” style in almost all of your JS oriented applications,not to mention coding like that is a “must-have” in NodeJS applications.

Next article,we will be experiencing a real world problem for detailed workaround.