Skip to content

Instantly share code, notes, and snippets.

@ByJC
Last active January 26, 2017 12:49
Show Gist options
  • Save ByJC/4838ca76ec3e2b6bd02cfed301f18f91 to your computer and use it in GitHub Desktop.
Save ByJC/4838ca76ec3e2b6bd02cfed301f18f91 to your computer and use it in GitHub Desktop.
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { ExampleService } from './example.service'
import 'rxjs/Rx';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/operator/mergeMap';
@Component({
selector: 'example',
templateUrl: 'example.component.html',
providers: [ExampleService]
})
export class ExampleComponent implements OnInit {
dataA: Array<Object> = [];
dataB: Array<number> = [];
dataC: Array<string> = [];
constructor(public http: Http, private exampleService: ExampleService) {}
ngOnInit() { }
usingFlatMap() {
this.exampleService.getDataA()
.flatMap(res => {
this.dataA = res;
return this.exampleService.getDataB();
})
.flatMap(res => {
this.dataB = res;
return this.exampleService.getDataC();
})
.subscribe(res => this.dataC = res);
}
usingForkJoin() {
Observable.forkJoin([
this.exampleService.getDataA(),
this.exampleService.getDataB(),
this.exampleService.getDataC()
])
.subscribe(([A,B,C]) => {
this.dataA = A;
this.dataB = B;
this.dataC = C;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment