Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.Js runs on the Google’s open-source scripting engine V8. Node.js is fast and lightweight. It uses the asynchronous mode of operation, event-driven Input/Output rather than using the traditional threads or separate threads for each process.
Node.js uses an event-driven, non-blocking Iinpu/Output model.
Blocking methods execute synchronously and non-blocking methods execute asynchronously.
Non-Blocking Example
Non-Blocking: It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line. The program calls the function and move to the next operation and does not wait for it to return.
Non-Blocking means Asynchronous. Asynchronous execution refers to execution that doesn't run in the sequence (Line by Line).
All of the I/O methods in the Node.js standard library provide asynchronous, which are non-blocking, and accept callback functions.
Note: In the non-blocking program the sum actually prints before the content of the file. This is because the program does not wait for the readFile() function to return and move to the next operation. And when the readFile() function returns it prints the content.
Blocking Example
Blocking: It refers to the blocking of further operation until the current operation finishes. Blocking methods are executed synchronously. Synchronously means that the program is executed line by line. The program waits until the called function or the operation returns.
Synchronous execution usually refers to code executing in sequence.
Synchronous vs Asynchronous
Synchronous (or sync) execution usually refers to code executing in sequence. In sync programming, the program is executed line by line, one line at a time. Each time a function is called, the program execution waits until that function returns before continuing to the next line of code. Asynchronous (or async) execution refers to execution that doesn’t run in the sequence it appears in the code. In async programming the program doesn’t wait for the task to complete and can move on to the next task. In the following example, the sync operation causes the alerts to fire in sequence. In the async operation, while alert(2) appears to execute second, it doesn’t.
Trending Tutorials