RxJS - 使用 Scheduler
调度程序控制订阅何时启动和通知的执行。
要使用调度程序,我们需要以下内容 −
import { Observable, asyncScheduler } from 'rxjs'; import { observerOn } from 'rxjs/operators';
这是一个工作示例,其中,我们将使用决定执行的调度程序。
示例
import { Observable, asyncScheduler } from 'rxjs'; import { observeOn } from 'rxjs/operators'; var observable = new Observable(function subscribe(subscriber) { subscriber.next("My First Observable"); subscriber.next("Testing Observable"); subscriber.complete(); }).pipe( observeOn(asyncScheduler) ); console.log("Observable Created"); observable.subscribe( x => console.log(x), (e)=>console.log(e), ()=>console.log("Observable is complete") ); console.log('Observable Subscribed');
输出
如果没有调度程序,输出将如下所示 −