Promise vs Callback in javascript

Promises and callbacks are very basic concepts and every javascript developer should know the difference.

Let's try to understand with a small example:

someWork() is a function that is going to take roughly around 2 seconds and we want to execute otherWork() function once someWork() is done. This can be achieved using a Callback or Promise.

Callback

Here we pass otherWork() function as an argument to someWork() function.

someWork() function will call otherWork() function after its work is done.

Promise

Here someWork() function return promise object which can be resolved(success) or rejected(error). We can call otherWork function in .then.

Share this with anybody you think would benefit from this. Have any suggestions? Feel free to message me on LinkedIn.

--

--

Software Architect, Full Stack Web developer, MEAN/MERN stack, Microservices, etc

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Riddhesh Ganatra

Software Architect, Full Stack Web developer, MEAN/MERN stack, Microservices, etc