天天看點

通過一個實際例子了解Angular rxjs Observable的異步行為

源代碼:

ngOnInit(): void {
    const a = new Observable((observer) => {
      observer.next(1);
      observer.next(2);
      observer.next(3);
      setTimeout(() => {
        observer.next(4);
        observer.complete();
      }, 1000);
    });
    console.log('just before subscribe');
    a.subscribe({
      next: x => console.log('got value ' + x),
      error: err => console.error('something wrong occurred: ' + err),
      complete: () => console.log('done'),
    });
    console.log('just after subscribe');
  }
      
通過一個實際例子了解Angular rxjs Observable的異步行為

繼續閱讀