What is Callback -?????

A Callback function is a function that is p-ed to another function(let’s call this another function) as a parameter and the callback function is executed inside the other function.

SHARE THIS POST :
What is Callback <i>-</i>?????
A Callback function is a function that is p-ed to another function(let’s call this another function) as a parameter and the callback function is executed inside the other function.

This article gives a brief introduction to the concept of Callback - and how it can be avoided.

 

Before going to Callback - first we understand what is Callback ???

 

A Callback function is a function that is p-ed to another function(let’s call this another function) as a parameter and the callback function is executed inside the other function.

 

A callback function is like a servant who, later fini-ng a job “call back to his master”(It’s like call me back when you are done)
 

Let’s begin with a simple example to demonstrate callbacks.

 

Now come to the Callback -

Callback - is the nesting of lots of callback functions. It can lead to code confusing and difficult to understand. Callback - is a phenomenon that affects a javascript developer. Better you can hire reactjs developers when he attempts to perform various asynchronous actions one after another.

 

How to avoid callback - problem??

 

Methods to deal with callback - problem.

  1.Using Promises.

  2.Using Async/Await

  

Using Promises:

According to MDN:

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

 

A promise can be one of these states.

1.pending:-This is the initial state of promise. Neither fulfilled nor rejected.

2.fulfilled:-In this state promise gets resolved.

3.rejected:-In this state promise gets rejected or promise failed.

 

Using  Async/Await:

Async: The functions with the “async” keyword indicates that the function always returns a Promise. If the function causes an error, then the Promise will be rejected and if the function returns a value then promise get resolved.

Await:Await keywords operates only inside async functions. This will pause async functions and wait for the promise to be resolved before moving on.Async/Await allows your asynchronous code written synchronously, making it readable.

Conclusion:- Callback - is not a problem it makes code confusing and difficult to understand.

There are few more solutions like modularization and generator functions to solve callback -

Problem. But async/await one of the best features to solve callback - problem it makes code clean, better error handling and simple debugging.