Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between callback and promise [duplicate]

Tags:

Possible Duplicate:
What are the differences between Deferred, Promise and Future in Javascript?

Can someone point out what are the differences are between callbacks and promises? When should one use promise etc?

Also links on how to create and use promises will be appreciated.

like image 784
NSS Avatar asked Jan 09 '13 19:01

NSS


1 Answers

Promises provide a more succinct and clear way of representing sequential asynchronous operations in javascript. They are effectively a different syntax for achieving the same effect as callbacks. The advantage is increased readability. Something like this

aAsync()   .then(bAsync)   .then(cAsync)   .done(finish); 

is much more readable then the equivalent of passing each of those individual functions as callbacks, like

Async(function(){     return bAsync(function(){         return cAsync(function(){             finish()         })     }) }); 
like image 159
Ben McCormick Avatar answered Sep 22 '22 09:09

Ben McCormick