Promises:

Promises provide a more structured way to work with asynchronous operations. They encapsulate the result or error of an asynchronous task and allow you to attach .then() and .catch() handlers to specify what should happen when the task is complete or encounters an error. we are in a complete control of that paradigm. we have brought many sanity to this control.

With promises, we simply uninvert the inversion of control problem, promises were designed to retain the control we usually have in our code.

How do they work ?

Special objects built into JavaScript that get returned immediately when we make a call to a web browser API/feature (e.g. fetch) that’s set up to return promises (not all are)

  • 💡 Promises act as a placeholder for the data we hope to get back from the web browser feature’s background work
  • 💡 We also attach the functionality we want to defer running until that background work is done (using the built in .then method)
  • 💡 Promise objects will automatically trigger that functionality to run
  • 💡 The value returned from the web browser feature’s work (e.g. the returned data from the server using fetch) will be that function’s input/argument

Promises in JavaScript work under the hood by managing the state of asynchronous operations and providing a structured way to handle their results or errors. Promises are implemented as a combination of JavaScript code and the event loop, which is the core of JavaScript’s concurrency model.