What is Promise?

Promise represents the result of an asynchronous operation. You can think of it as a placeholder for a result that you’ll eventually receive.

Why Promise?

To solve the problem of callback hell.

Callback hell is any code where the use of function callbacks in asynchronous code becomes difficult to follow. Whenever we have loads of nested callbacks, code becomes harder to read, test, and debug. This also leads to the pyramid of doom.

Here is an example of callback hell.

function a() {
  setTimeout(() => {
    setTimeout(() => {
      setTimeout(() => {
        setTimeout(() => {
          setTimeout(() => {
            //do something
          }, 1000);
        }, 1000);
      }, 1000);
    }, 1000);
  }, 1000);
}

#web-development #javascript #programming

Understanding Promises in JavaScript
1.95 GEEK