Observable - 使用Observable
优质
小牛编辑
136浏览
2023-12-01
Observable.of(1, 2, 3).subscribe(
doSomething,
reject,
resolve);
});
The forEach pattern is useful for a sequence of events you only expect to happen once.
export class MyApp {
private data: Observable<Array<number>>;
private values: Array<number> = [];
private anyErrors: boolean;
constructor() {
}
init() {
this.data = new Observable(observer => {
setTimeout(() => {
observer.next(42);
}, 1000);
setTimeout(() => {
observer.next(43);
setTimeout(() => {
observer.complete();
}, 3000);
this.status = "Started";
});
let subscription = this.data.forEach(v => this.values.push(v))
.then(() => this.status = "Ended");
}