Introduction to asynchronous in NodeJS !

NodeJS is an asynchronous event driven framework, which removes many things of related frontend in Javascript and adds many libraries such as http,.. which supports to build web applications in the server side. Asynchronous is the most interesting features of NodeJS and it make many websites built in NodeJS have high responsibility and handle more requests than other frameworks. The benchmarks can be seen in [1]. In this introduction, I will explain in the simplest way about Asynchronous.

Asynchronous I/O, or non-blocking I/O can be understood that an process do one job while other jobs can run concurrent and not wait until first job complete. The first job can have a callback that can be a function called after it finish.

An attractive example of callback function is the built-in function setTimeout:

setTimeout(function(){
    console.log("5s later");
}, 5000)

console.log("Start to count 5s")

Output:

Start to count 5s
5s later

As you can see, the line Start to count 5s normally will be showed after the function setTimeout finish, but now is the world of Nodejs, the console.log("Start to count 5s") will start after setTimeout started.

The function in the parameter of setTimeout is called by callback function, it will run after the setTimeout finished. Through 2 examples, I guess you can imagine the big picture of Asynchronous and CallBack function yet :)