Difference Between Promises & Callbacks

Promises are not Callbacks.

A Promise is a value which may be available in future or not. A Callback is a function which we call inside other function. Normally callback runs after the parent function completes its operation. Let’s say the function is sending AJAX query, we can use callback function in a way so, when the AJAX request gets response, we can use the response in the function to do some other operation.

In case you are interested to know more about Promises, I have published an article earlier on the same.

Syntax for both Promises and Callbacks.

Promise

Callback

If you compare the Promises code, it is much more readable then Callback function code.

Now let’s assume, if you need to debug error, with Callback function it will be hectic and need to go step-by-step in debug mode on browser but with Promise it will be much easier as shown in the code above. It will be like try-catch block.

Just think you need to handle 2-3 AJAX request and return values once all the request returns the values with Callback functions. It will be really nightmare. Promises are better when it comes to handle more then one AJAX requests. The code be much more easier and easy to understand.

In above code the resultant code will execute only when all 3 request are done and if any of the request fails it will go to the Catch block.

Pretty Straight…! Do let me know if this will help in comment box below.

Ashish Panchal

Web/ UI & Front-end developer based in Ahmedabad, GJ, India. Here to help/ discuss community to spread web awareness.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *