angular wait all subscriptions 1

angular wait all subscriptions

// Concat 
let first = Observable.timer(10,500).map(r => {
  return {source:1,value:r};
}).take(4);
let second = Observable.timer(10,500).map(r => {
  return {source:2,value:r};
}).take(4);
first.concat(second).subscribe(res => this.concatStream.push(res));
// This will merge the two but you will receive the first observable result before the second:
// 0 1 2 3 0 1 2 3

// Merge
let first = Observable.timer(10,500).map(r => {
  return {source:1,value:r};
}).take(4);
let second = Observable.timer(10,500).map(r => {
  return {source:2,value:r};
}).take(4);
first.merge(second).subscribe(res => this.mergeStream.push(res));

// You will get:
// 0 0 1 1 2 2 3 3

// Fork Join
let first = Observable.of({source:1,value:1});
let second = Observable.of({source:2,value:1});
Observable.forkJoin(first,second).subscribe((res:Array) => this.forkJoinStream = res);

// FlatMap
let first = Observable.of(10);
first.flatMap((operand1) => {
  return Observable.of(operand1 + 10);
})
.subscribe(res => this.flatMappedStreams = {msg: '10 + 10 = ' + res});

Here is what the above code is Doing:
1. We create two observables, one that emits a value of 10 and another that emits a value of 20.
2. We then use the flatMap operator to add the two values together.
3. We then subscribe to the result and display the result in the UI.

Similar Posts