RxJS – 使用调度程序
调度程序控制订阅开始和发送通知的时间。
要使用调度程序,我们需要以下内容:
import { Observable, asyncScheduler } from 'rxjs';
import { observeOn } 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');
输出
如果不使用调度程序,输出将如下图所示:
广告