Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





why use node.js

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.

File Name : non-blocking.js

const getUser = require('./src/getUser');
getUser(1,(user) =>{
console.log(user)
})
getUser(2,(user) => {
console.log(user)
})
const sum = 5+3
console.log(sum)

File Name :

const fs = require('fs');

const filepath = 'text.txt';

// Reads a file in a asynchronous and non-blocking way
fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
// Prints the content of file
console.log(data);
});


// This section calculates the sum of numbers from 1 to 10
let sum = 0;
for(let i=1; i<=10; i++){
sum = sum + i;
}

// Prints the sum
console.log('Sum: ', sum);

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.

File Name : blocking.js

const getUserSync = require('./src/getUserSync');
const userOne = getUserSync(1)
console.log(userOne)

const userTwo = getUserSync(2)
console.log(userTwo)


const sum = 5+3
console.log(sum)

File Name :

const fs = require('fs');

const filepath = 'text.txt';

// Reads a file in a synchronous and blocking way
const data = fs.readFileSync(filepath, {encoding: 'utf8'});

// Prints the content of file
console.log(data);

// This section calculates the sum of numbers from 1 to 10
let sum = 0;
for(let i=1; i<=10; i++){
sum = sum + i;
}

// Prints the sum
console.log('Sum: ', sum);

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.

File Name :

// Synchronous: 7,8,6
alert(7);
alert(8);
alert(6);

// Asynchronous: 7,6,8
alert(7);
setTimeout(() => alert(8), 0);
alert(6);





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here