22
loading...
This website collects cookies to deliver better user experience
Note: These series of articles mainly revolve around client-side JavaScript that runs in the browser. The core concepts, for the most part, also apply as-is to server-side JavaScript using NodeJS.
var first = "I'll be executed first";
var second = "I'll be executed next";
console.log("I'll be executed last");
alert
simulating such a time-consuming operation.// simulate a time-consuming operation.
alert("I'm going to freeze this browser!😈");
console.log("Yayy! The alert is gone.🎉");
alert()
replaced with setTimeout()
.// asynchronous
setTimeout( function onTimeout() {
console.log("I'll run asynchronously so I won't freeze anything.😇");
}, 1000);
console.log("Woo hoo!! No more freezing!🎉");
/*
Woo hoo!! No more freezing!🎉
I'll run asynchronously so I won't freeze anything.😇
*/
setTimeout()
is asynchronous, the program is not blocked and JS proceeds ahead to execute the statements that come after it. After the 1 second timeout, the asynchronous code inside the callback onTimeout()
callback is executed. If setTimeout()
was not asynchronous, the program would pause and the browser would freeze for a whole second just like in the previous example that used alert()
. setTimeout()
? That is the job of the environment in which JS runs. On the client-side that environment is your browser while on the server-side, its NodeJS.onTimeout()
, that contains our asynchronous code. In the meantime, JS will keep executing other code. After 1 second, the browser will tell JS, "Hey! the timeout has completed so you should invoke this callback you gave me.". XMLHttpRequest
for this.// asynchronous
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function onLoad() {
console.log("I'll run asynchronously so I won't block anything.😇");
});
xhr.open("GET", "https://api.github.com/users/saurabh-misra");
xhr.send();
console.log("Woo hoo!! No more freezing!🎉");
/*
Woo hoo!! No more freezing!🎉
I'll run asynchronously so I won't block anything.😇
*/
setTimeout()
scenario, JS will initialize the network request and hand things over to the browser. It will also tell the browser to call the onLoad()
function once the request is complete. The browser will take care of sending the request and waiting for the response. In the meantime, JS will continue to execute the rest of the program and will print the text provided to console.log()
. When the request completes, the browser will tell JS to execute onLoad()
.var btn = document.getElementById( "btn" );
// asynchronous
btn.addEventListener( "click", function onButtonClick(){
console.log( "I'll run asynchronously so I won't block anything.😇" );
});
console.log("Woo hoo!! No more freezing!🎉");
/*
Woo hoo!! No more freezing!🎉
I'll run asynchronously so I won't block anything.😇
*/
button
element and passes it the onButtonClick()
handler. When the user clicks the button at some point in the future, the browser informs the JS engine about it and tells it to invoke the handler.onTimeout()
, onLoad()
and onButtonClick()
are all examples of asynchronous callbacks. The idea is that these functions will be called back when the asynchronous operation completes.const cars = ['BMW', 'Mercedes', 'Audi'];
// synchronous
cars.forEach(function displayCar(car, index){
console.log( (index+1) + '. ' + car );
});
/*
1. BMW
2. Mercedes
3. Audi
*/
displayCar()
is passed in as an argument to the forEach()
function. But forEach()
is synchronous and does not initiate an asynchronous operation. So the code inside displayCar()
is executed synchronously. So even though displayCar()
is a callback function, it is not an asynchronous callback function.