Promises makes chaining asynchronous tasks a little prettier than its deeply nested callback counterpart. For example:
myPromise .then(doTheFirstThing) .then(doTheSecondThing) .catch(failHandler)
This chainability is argued to be more easily reasoned about. Promises also come with some guarantees:
- Callbacks won’t be called before completion of the current run of the event loop, which avoids race conditions.
- Callbacks added with
then()even after the success/failure of async operation will be called.
- Multiple callbacks can be added with
then(). They will be executed in order.
- Callbacks are called one time at most.
catch(failureCallback) is short for
Learned from MDN