Introduction
RxJS is a popular library for reactive programming in JavaScript. It provides a powerful way to handle asynchronous events, which are common in web development. In this podcast, we'll discuss the future of RxJS 6 & 7 and its roadmap for new operators.
What's New in RxJS 6 & 7?
RxJS 6 was released in 2018 and introduced several changes and improvements. One of the most notable changes was the introduction of lettable operators, which allowed for tree-shaking and better performance. Additionally, RxJS 6 removed some deprecated features and made it easier to use with other libraries.
RxJS 7, which is currently in beta, builds on the improvements from RxJS 6 and introduces some new features as well. One of the major changes in RxJS 7 is the removal of the creation methods like of
and from
. Instead, they are replaced with static creator functions like fromArray
, fromPromise
, and fromEvent
.
The Roadmap for New Operators
In this podcast, we'll also be discussing the roadmap for new operators in RxJS. The RxJS team has identified several areas where they want to improve the library, including error handling, scheduling, and cancellation.
Error Handling
Error handling has always been an important aspect of any programming language or library, and RxJS is no exception. In RxJS 7, the team is introducing new error handling operators like catchError
, retryWhen
, and onErrorResumeNext
.
These operators will make it easier to handle errors in your observables and provide more control over how errors are handled. For example, catchError
allows you to catch an error and return a new observable instead of throwing an error.
import { of } from 'rxjs'; import { catchError } from 'rxjs/operators'; const source = of('hello', 'world', '#$%@'); source.pipe( map(x => x.toUpperCase()), catchError(err => of('error occurred', err.message)) ).subscribe(x => console.log(x));
Scheduling
RxJS 6 introduced a new scheduler system, which allowed you to control how observables emit their values. In RxJS 7, the team is continuing to improve the scheduler system by introducing new operators like observeOn
.
observeOn
allows you to specify on which scheduler to emit the values of an observable. This allows you to control the thread or process on which the values are emitted, which can be useful for optimizing performance.
import { interval } from 'rxjs'; import { observeOn } from 'rxjs/operators'; interval(1000).pipe( observeOn(someScheduler) ).subscribe(x => console.log(x));
Cancellation
Cancellation is another important aspect of reactive programming, and the RxJS team is working on improving cancellation in RxJS 7. One of the new operators that they're introducing is takeUntil
.
takeUntil
allows you to specify a second observable, and emits the values from the first observable until the second observable emits a value. This can be useful for canceling a long running observable when a user navigates away from a page.
-- -------------------- ---- ------- ------ - --------- ----- - ---- ------- ------ - --------- - ---- ----------------- ----- ------ - --------------- ----- ------ - ------------ ------------ ----------------- ------------- -- ----------------
Conclusion
RxJS is a powerful library for reactive programming in JavaScript, and its future looks bright with the upcoming improvements in RxJS 6 & 7. The new operators that are being introduced will make it easier to handle errors, control scheduling, and improve cancellation in your observables.
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/7038