How Promises solve the issue of callbacks in JS

How Promises solve the issue of callbacks in JS

Promises are used to handle Async operations. But before promises async operations used to be handled by callback functions. Then what was the need of promises when async operations could be handled by callback functions . Because of the major issues in callback functions promises have been introduced which solves all the major issues allowing us to make async operations more easily.

Major issues in handling async operations with callback functions :

  1. Inversion of Control
  2. Callback Hell

  3. Inversion of Control : It simple terms it means giving the control of our code to someone else. Suppose we are making a createOrder() api call and we are attaching proceedToPayment() callback function with it .Here we are giving the complete control of our callback func to the api call (3rd party) which we have no control on , so we never know whether our code gets executed or not or it is executed twice because the control is not in our hand and that's what is Inversion of contral.

How promises solve the issue of inversion of control

In case of promises the createOrderApi() will not take the callback func instead it is going to take cart details and return us a Promise

Now promise is nothing but an empty object . Whenever Js engine will execute the createOrderApi() (which is a async operation) it is going to return a promise which is an object with some empty value of undefined. Now since create order is an async operation it is not going to return the data as soon as it called( it takes some time) it returns us an empty value and js engine continues to execute further code After some time(whatever times takes to execute the async operation ) this promise will be automatically filled with data. Now once the data is filled we consume it with the help of .then method. two arguments; the first argument is a callback function for the fulfilled case of the promise, and the second argument is a callback function for the rejected case. Here we are call the callback function only when we have the data that means the complete control is in our hand .

  1. Callback Hell : In short, it is when your callbacks have been nested multiple levels to the point it becomes unmanageable. This can happen in any programming language and is more common in asynchronous operations. Readability is one thing, but callback hell can cause other scoping issues.

What is promise chaining in JavaScript?

You just learned a callback hell indicates unmanageable nested levels of callbacks. With that said, one way to solve the issue is to make the callbacks NOT nested anymore. Make it shallow!

Promise chaining in JavaScript is when multiple then and catch methods are called successively to completely removes the nested levels, while still maintaining the intended outputs. You can say it is one way to refactor your codebase. then and catch are methods of a Promise object, so to create a chain, a callback in the then method must return a new Promise.

Conclusion:

This was all about the Execution context . If you've reached this point, Thanks for taking your time and reading this article, hope you've enjoyed reading it and found it helpful. Do not hesitate to share your feedback.