DEV Community

Discussion on: What are Callbacks in Javascript and how to use them

Collapse
 
dpkahuja profile image
Deepak Ahuja 👨‍💻 • Edited

The intent of promises is not to get rid of "pyramid of doom" as promise themselves can have it, It has to do with the trust issue and Inversion of control in callbacks, as a callback cannot be trusted to be called either once or multiple times or not at all when being passed to an external API. Either with the error or without error or both. The error first callback is a mere convention in nodejs.
Promises solved this problem as they can only have a finite number of states in output (fulfilled in case success or rejected in case of error) and can only be done once in either.

It also inverts the "Inversion of control" by making an event-based system in which we don't have to depend on external API to call the passed callback function, hence reducing testing overhead and more control over callback. The promise API is easy to reason about than callbacks.

Collapse
 
adityasridhar profile image
Aditya Sridhar

Thanks Deepak for explaining more on promise in detail.