AngularJS developers are familiar with $q and $http services to run functions asynchronously and execute remote calls . In most cases $q is used to retrieve data from http to render view. In this post I will try to implement “Angular 1 $q Promise” patterns with “Angular 2 RxJS Observable”. I assume that the reader is familiar with Angular 2 and Http service.

toPromise

Some people suggest to use toPromise any time you get observables and forget about Observables but I don’t think this is a good strategy in long term if you are serious about Angular 2. Here is an example of toPromise. Angular 2 Http service returns an Observable that is immediately converted to promise.

Not toPromise

Let’s try to keep observables. $q allows you to chain promises using “then” method. Observable operators are also chainable. Let’s try to rewrite simple promise chains using observables.

  1. Map. The first chain uses one http call, after multiple the response to 10 and assign the result to a view variable that is used to render.

Analog of $http in Angular 2 is Http service that returns a Observable with raw response. We need to transform the response to JSON object, transform the result by multiplying to 10 and assigning the result to view variable. For simple transformation in RxJS “map” operator is used that takes a simple value and returns another simple value. To get the value from observable “subscribe” operator is used. So here is the version with Observable:

2. SwitchMap. Let’s try another example when we need to call one http service, analyze the response, call another http service and display data in view.

“Then” method can return a simple value but also another Promise. With Observable we have to use a flatterning operation when “map” operator returns an Observable. There are several flatterning operators in RxJS, the most save one for simple http is switchMap. SwitchMap returns an observable (Angular 2 http observable).

3. ForkJoin. Another common case is $q.all. $q.all is used to create multiple http calls simultaneously and only when all requests get resolved, update the view with combine result.

Here is a version with Observables. forkJoin operator takes several observables (in our case http calls). The last argument of forkJoin is a function that accepts responses returned by all observables and allows to combine them to one object. Again we use subscribe to get result of observable chain.

4. Of. In Angular 1 in order to create a resolved promise $q.when is used. It is often used to create a starter Promise. For example, if we already have a modal object we create a resolved Promise with model. Otherwise, we call http service to get a model

The same login with Observable is:

5. Empty the chain. It is was always an issue how to cancel the promise chain in the middle of executing. With Observable it is a simple “empty” operator. It completes the observable chain and we do not need to handle any errors.

We checked the value returned from the first http call and completed Observable with Observable.empty(). After complete, the next http was not fired.

6. Reject. $q provides a method to create a rejected promise.

RxJs has throw operator that does the same:

7. Catch. With promise chains we use catch method to handle any exceptions and rejected promises.

RxJS also has catch operator that is called when an error is happen during the execution and should return a new Observable to replace the error.

Replace with default object:

Complete observable:

Throw another error:

8. Error handeling. Promise’s “then” accepts three functions. The second is a error handler:

Also subscription operator provides an error function that can be used to handle errors:

8. Finally. Finally method is used to perform an action when Promise or Observable resolved/reject/completed/errorred.

Here is the same with RxJS

9. Do. “Then” is used in Promise to perform an effect. “Then” in this case should return an input argument.

RxJS provides a “do” operator that allows to perform an effect. The do operator should not return anything and the observable chin continues like “do” was not there.

Conclusion

I hope it will help you not to do toPromise and enjoy Observables. When you get comfortable with these simple chains, you can start mixing observables from Ngrx, DOM, Angular 2 Form, Route..

Front-End developer working as an independent contractor with Angular and React in Toronto, Canada.