As we all know that Javascript has completely changed the Web development World. There was a time when Javascript was only used for front end validations and other basic tasks, But after Node.js and other front end frameworks like Angular, React etc, Javascript is used for complete web development. You can understand the importance of Javascript from the fact that It is one of the most demanding languages today.
In this article, I will explain the different ways to make an asynchronous call in Javascript.
Imagine requesting some data from an API. Depending upon the situation the server might take some time to process the request while blocking the main thread making the web page unresponsive.
That’s where asynchronous JavaScript comes into play. Using asynchronous JavaScript (such as callbacks, promises, and async/await), you can perform long network requests without blocking the main thread.
setTimeout(function (){
console.log('console when five seconds have passed.');
}, 5000);
console.log('End Line');
If we run the above program on the browser then, It will print like below:
second line
console when five seconds have passed.
The setTimeout()
function takes a function to call when the timeout has elapsed, and a number of milliseconds to wait before calling that function. When we make a call to setTimeout()
it is a call to a C++ API. This gets moved off of the execution stack and when the timeout completes, the callback gets pushed into the queue. Once the execution stack is empty, the event loop will pull the callback from the queue and add it to the execution stack.
It is very oldest approach for making an Asynchronous call in javascript. In this approach we mainly pass the last parameter as the callback function. Let's have a look at example below:
The above example shows the another common pattern for callbacks, and that is the first parameter of the callback function is an error result. This allows the callback to check for an error result first and go ahead and throw or handle the error before trying to read a result it probably doesn’t have.
Drawbacks of Callback
To overcome the issues with Callbacks, Promise became very popular.
A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending. Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.
Promises were introduced to fix a lot of the problems with using callbacks to do asynchronous operations.
Basic Syntax
Let's create own Promise to create database connection and call it.
In the above example, the MongoClient.connect()
function is wrapped to return a promise while the callback is being processed. Once the callback is called, it either resolves or rejects the promise. When we call the connectToMongo()
function, we get that promise back. Then chain then()
function calls onto the promise. Each then returns a promise, and in the first then()
the promise resolves with the database query results. If there is an error and the promise is rejected, I handle it here with the catch()
function.
It also works like promises behind the scene, But syntax is shorter than the promise and code becomes more readable with Async/Await.
Let's understand it with example:
For using Async/Await. It is necessary to use Async keyword before the function keyword and await is used where we are making the call. In the above example, connectToMongo is a method which is returning promise which is handled with the await. So await can be used only at the function which is returning a promise. That's why I written earlier that Async/Await works like Promises behind the scene.
Click here to find Sample Applications on different javascript frameworks.
Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share with your friends.
Thank You!