Skip to content

Instantly share code, notes, and snippets.

@ByJC
Created January 26, 2017 12:29
Show Gist options
  • Save ByJC/823d9e15747b717d820a516f7c1dc01c to your computer and use it in GitHub Desktop.
Save ByJC/823d9e15747b717d820a516f7c1dc01c 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 'rxjs/Rx';
import 'rxjs/add/operator/mergeMap';
@Component({
selector: 'example-flatMap',
templateUrl: 'flat-map.component.html'
})
export class FlatMapComponent implements OnInit {
dataA: Array<Object> = [];
dataB: Array<number> = [];
dataC: Array<string> = [];
constructor(public http: Http) {}
ngOnInit() {
this.http.get('http://www.server.com/dataA')
.map(res => res.json())
.flatMap(res => {
this.dataA = res;
return this.http.get('http://www.server.com/dataB').map(res => res.json());
})
.flatMap(res => {
this.dataB = res;
return this.http.get('http://www.server.com/dataC').map(res => res.json())
})
.subscribe(res => this.dataC = res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment