Getting started with asynchronous
Remarks#
Asynchronous is a programming pattern which provides the feature of non-blocking code i.e do not stop or do not depend on another function / process to execute a particular line of code.
Asynchronous is great in terms of performance, resource utilization and system throughput. But there are some drawbacks:
- Very difficult for a legacy programmer to proceed with Async.
- Handling control flow is really painful.
- Callbacks are dirty.
If you are a function-oriented programmer, then it would be little difficult for you to grasp asynchronous programming. However, if you are familiar with multithreading
in Java, then this is similar to that.
Installation or Setup
Detailed instructions on getting asynchronous set up or installed.
File Reading in Node js
var fs = require("fs");
fs.readFileSync(‘abc.txt’,function(err,data){ //Reading File Synchronously
if(!err) {
console.log(data);
}
//else
//console.log(err);
});
console.log("something else");
Here, the program was waiting while reading the file. It will not go further before completing the read operation, which is an example of blocking code. But ideally, we should proceed further while the program was reading the file and once it is done we should go back and process that. That is what happening in the following code.
var fs = require("fs");
fs.readFile(‘abc.txt’,function(err,data){//Reading file Asynchronously
if(!err) {
console.log(data);
}
});
console.log("something else");
Here, the program is not waiting, hence you see the console first and file contents later.