Promises
Promises are another useful asynchronous concept available in Angular. Conceptually, promises implement a totally different pattern. A Promise
is a value that will be resolved or rejected in the future. Like the Observer
pattern, they can be used to manage async programming. So, why bother to have two concepts to do the same thing? Well, the verbosity of Observer
allows one thing that Promise
does not: unsubscribe. The main difference that may lead to a decision about which one to use is the ability of Observable
to catch many subsequent asynchronous events, while Promise
can manage a single asynchronous event. To emphasise the differences between Observer
and Promise
, we will take the same example as before, fetching movies from a JSON API.
The AngularObservableAppComponent
component will make an asynchronous call to the IMDBAPIService
and, upon the answer, will update the HTML view.
Here's the fetchOneById
method using Promise
instead of Observable
:
/** , * Return a Promise...