Switch from a Promise to an Observable

master
Matt Huntington 9 years ago
parent eb021c22fe
commit 1dedf84586

@ -4,6 +4,7 @@
1. Describe publish/subscribe model and how it relates to Observables 1. Describe publish/subscribe model and how it relates to Observables
1. Demonstrate when a normal promise is not optimal 1. Demonstrate when a normal promise is not optimal
1. Switch from a Promise to an Observable
## Describe publish/subscribe model and how it relates to Observables ## Describe publish/subscribe model and how it relates to Observables
@ -27,3 +28,20 @@ Firstly, we want it to search as the user types into the input (like how google
``` ```
Test it out. In the network tab of your Chrome Developer Tools, see how a request goes out for every letter? And they don't always come back in order, either. With Observables, we can fix this. Test it out. In the network tab of your Chrome Developer Tools, see how a request goes out for every letter? And they don't always come back in order, either. With Observables, we can fix this.
## Switch from a Promise to an Observable
In `src/app/search/search.component.ts` edit the `findCharacter` method of `SearchComponent`.
```javascript
findCharacter(name){
this.http.get('http://swapi.co/api/people/?search=' + name)
.subscribe(response => this.results = response.json().results);
}
```
Since we aren't using `toPromise` anymore, we can eliminate this line of code in `src/app/search/search.component.ts`
```javascript
import 'rxjs/add/operator/toPromise';
```

@ -1,6 +1,5 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http'; import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Component({ @Component({
selector: 'app-search', selector: 'app-search',
@ -17,8 +16,7 @@ export class SearchComponent implements OnInit {
findCharacter(name){ findCharacter(name){
this.http.get('http://swapi.co/api/people/?search=' + name) this.http.get('http://swapi.co/api/people/?search=' + name)
.toPromise() .subscribe(response => this.results = response.json().results);
.then(response => this.results = response.json().results);
} }
ngOnInit() { ngOnInit() {

Loading…
Cancel
Save