Observable - 使用Observable

优质
小牛编辑
131浏览
2023-12-01
  1. Observable.of(1, 2, 3).subscribe(
  2. doSomething,
  3. reject,
  4. resolve);
  5. });
  6. The forEach pattern is useful for a sequence of events you only expect to happen once.
  7. export class MyApp {
  8. private data: Observable<Array<number>>;
  9. private values: Array<number> = [];
  10. private anyErrors: boolean;
  11. constructor() {
  12. }
  13. init() {
  14. this.data = new Observable(observer => {
  15. setTimeout(() => {
  16. observer.next(42);
  17. }, 1000);
  18. setTimeout(() => {
  19. observer.next(43);
  20. setTimeout(() => {
  21. observer.complete();
  22. }, 3000);
  23. this.status = "Started";
  24. });
  25. let subscription = this.data.forEach(v => this.values.push(v))
  26. .then(() => this.status = "Ended");
  27. }