RxJS version is available now available.
New Fetch Observable
RxJS now provides built-in support for using native JS fetch API, including ability to abort requests with AbortController baked-in.
import { of } from 'rxjs';
import { switchMap, catchError } from 'rxjs/operators';
import { fromFetch } from 'rxjs/fetch';
const users$ = fromFetch('https://reqres.in/api/users').pipe(
switchMap(response => {
if (response.ok) {
return response.json();
} else {
return of({ error: true, message: `Error ${response.status}` });
}
}),
catchError((error) => of({ error: true, message: error.message }))
);
users$.subscribe({ next(data) { ... }, complete() { ... } });
forkJoin Improvements
forkJoin now takes dictionary of sources
import { forkJoin, timer } from 'rxjs';
import { take, mapTo } from 'rxjs/operators';
const source = forkJoin({
todos: timer(500).pipe(mapTo([{ title: 'RxJS'}])),
user: timer(500).pipe(mapTo({ id: 1 }))
});
source.subscribe({
next({ todos, user }) { }
});
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]).
Partition Observable
The current partition observable operator is deprecated in faor of new creation observable, named partition.
The partition observable splits the source observable into two observable’s, one for values which satisfy given predicate, and other for values which don’t.
import { fromEvent, partition } from 'rxjs';
const clicks$ = fromEvent(document, 'click');
const [clicksOnH1$, clicksElsewhere$] =
partition(clicks$, event => event.target.tagName === 'H1');
clicksOnH1$.subscribe({
next() { console.log('clicked on h1') }
});
clicksElsewhere$
.subscribe({
next() {
console.log('Other clicked')
}
});
combineLatest Deprecation
This version deprecates all combineLatest signature except CombineLatest([a, b, c]). You can read reason for change here.
Schedulers:
Add the scheduled creation function to create a scheduled observable of values. The scheduled version of from, range, et al. have been depracated.
import { of, scheduled, asapScheduler } from 'rxjs';
console.log(1);
// DEPRECATED
// of(2, asapScheduler).subscribe({
// next(value) {
// console.log(value);
// }
// });
scheduled(of(2), asapScheduler).subscribe({
next(value) {
console.log(value);
}
});
console.log(3)