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');
输出
如果没有调度程序,输出将如下所示 -
