Asked 7 years ago
18 Jan 2017
Views 1231
jaydeep

jaydeep posted

what is async call in JavaScript ?

what is async call in JavaScript ? is it means it call is not synchronized , is that so ? how can be implemented with JavaScript ?
Rasi

Rasi
answered Nov 30 '-1 00:00

synchronized call means thread apply to that call and never release thread until call finished .

async call means thread not apply delicately to that call and try to call next sequence of code until get response from current call . so there is no surety about the ordering in the async call .

JavaScript single threaded script means it take care one call in one time so in async call it give other to priority and call them in meanwhile of first call get response or get executed.

Example ::


setInterval(function (){
console.log('one second passed - setInterval called');

},1000)
setTimeout(function (){
console.log('setTimoeut  called');

},2000)


output ::
one second passed - setInterval called
setTimoeut called
one second passed - setInterval called

JavaScript have all function async priority . as you can see above it run setInterval than it should go to loop for a second and it run untill it get released by first function . but in between it call setTimeout and again back to loop of the setInterval . so in above scenario you cant say it give output exact 2 sec. if setTimeout take some more seconds it finish it than go back to interval . so async call never give assurance of execution order

noob

noob
answered Nov 30 '-1 00:00

function call sometime cause some error like RangeError: Maximum call stack size exceeded or call stack overflow issue , solution of this problem is async call because async call put on halt until himself until get result from timer or I/O operation.which remove risk of call stack overflow.


but async function not used when one function call depend on the first call result. because async call have no timing or order surety , in that situation one should use sync function call
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

Node JS with sync and async call
suppose Node JS have readFile which is great example of async call , Node JS have other function readFileSync which is example of Sync call .


var fs =require('fs');
var crypto = require('crypto');
var file=fs.readFileSync('abc.txt');
crypto.createHash('md5').update(file).digest('hex')
//other code

var file=fs.readFileSync('abc.txt'); is sync call means it not run other bellowed code untill it get done with readFile , which is good in our case because in the next time using file content which is danger if it not fully read.
if we use readFile than it async call .With the asynchronous methods there is no guaranteed execution ordering So need to provide callback function which will be called when function done with read file

var fs =require('fs');
var crypto = require('crypto');
 fs.readFile('abc.txt','utf-8',function(err,data){
if(err) throw err;
crypto.createHash('md5').update(data).digest('hex')

});
//other code


what if do not use call back function to collect content.

var fs =require('fs');
var crypto = require('crypto');
var content=fs.readFile('abc.txt','utf-8');
console.log(content);//undefined

file content is undefined in that case.

Post Answer