constructor( private route: ActivatedRoute, private http: Http ){ // Observe parameter changes let paramObs = route.paramMap; // Fetch data once let dataObs = http.get('...'); // Subscribe to both observables, // use both resolved values at the same level }
Is there something similar to forkJoin
that triggers whenever a parameter change is emitted? forkJoin
only works when all observables have completed.
I just need to avoid callback hell, any alternative that complies is welcome.
concat() which will handle each observable in sequence.
forkJoin Improvements Moreover, there is one deprecation — forkJoin(a, b, c, d) should no longer be used; Instead, pass an array such as forkJoin([a, b, c, d]) .
combineLatest is similar to forkJoin, except that it combines the latest results of all the observables and emits the combined final value. So until each observable is completed, the subscription block emits the result.
As forkJoin only completes when all inner observables complete, we must be mindful if an observable never completes.
There are several options:
Use take(1)
with forkJoin()
to complete each source Observable:
forkJoin(o1$.take(1), o2$.take(1))
Use zip()
and take(1)
to emit only when all source Observables have emitted the same number of items:
zip(o1$, o2$).take(1)
Use combineLatest()
to emit when any of the source Observables emit:
combineLatest(o1$, o2$)
Jan 2019: Updated for RxJS 6
A small trick to avoid breaking of observable subscriptions if any one of the observable fails.
import { throwError, of, forkJoin } from "rxjs"; import { catchError, take } from "rxjs/operators"; //emits an error with specified value on subscription const observables$ = []; const observableThatWillComplete$ = of(1, 2, 3, 4, 5).pipe(take(1)); const observableThatWillFail$ = throwError( "This is an error hence breaking the stream" ).pipe(catchError((error) => of(`Error Catched: ${error}`))); observables$.push(observableThatWillComplete$, observableThatWillFail$); forkJoin(observables$).subscribe(responses => { console.log("Subscribed"); console.log(responses); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With