Callback vs Promises vs Async Await
The word Callback, Promises, and Async Await might be intimidating to hear when you are just beginning to master JavaScript concepts. This post is here to help you have a better understanding of these functions in a short time. Now, let's begin mastering these useful concepts.
Callback Function
In Javascript every function is a first-class object, so a function is an object and can be used like any other object. This allows a function to be passed into another function as an argument, which is then invoked inside the outer function to complete an action.
Example of a synchronous callback function.
How does it work?
We can pass functions like variables and return them in function and use them in other functions. When we pass a callback function as an argument we are passing only function definition, we are not executing the function in parameter or we aren’t passing a function with trailing function executing within parenthesis (). And since the containing function has a callback function in its parameter as a function definition, it can execute anytime.
Asynchronous Callback Function
In this example, we are going to use the setTimeout() method to mimic the program that takes time to execute, such as data coming from the server.
The setTimeout() method executes a block of code after the specified time. Here,the greet() function is called after 2000 milliseconds( 2 seconds). During the wait, sayName('John') is executed. That is why Hello John is printed before Hello World.
The above code is executed asynchronously(the second function sayName() does not wait for the first function greet() to complete ).
Synchronous Callback Function
In the above program, the code is executed synchronously. The sayName() function is passed as an argument to the greet() function.
The setTimeout() method executes the greet() function only after 2 seconds. However, the sayName() function waits for the execution of the greet() function.
Benefits of CallBack
- CallBack functions help you write more clean and readable code.
- DRY — Do not repeat yourself
- Have better maintainability
It is very important to know that callback functions can be used for
- Event handlers/Listeners
- For asynchronous execution
Callback Hell
CallBack Hell is when you have too many nested callbacks and it makes the code very difficult to read. When the function tree created becomes too large, the code becomes incomprehensible sometimes and is not easily refactored. Callback functions can be used for short asynchronous operations but for larger sets it is advised to use Promises.
Let's see what Promises are in the next sub-heading
Promises
I promise to do this whenever that is true. If it isn't true, then I won't.
Meme Alert !!
A promise is used to handle the asynchronous result of an operation. This sounds exactly like callbacks, but the important differences are in the usage of Promises. Instead of providing a callback, a promise has its own methods which you call to tell the promise what will happen when it is successful or when it fails.
Promises have a few advantages over callbacks:
- Make the async code easier to read.
- Provide combined error handling.
- Better control flow. You can have async actions execute in parallel or series.
The methods a promise provides are then() for when a successful result is available and catch() for when something went wrong.
A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states:
- Fulfilled: resolve() was called
- Rejected: reject() was called
- Pending: not yet fulfilled or rejected
0 Comments