This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Observable } from 'rxjs'; | |
| import { retry } from 'rxjs/operators'; | |
| const observable = new Observable<number>(observer => { | |
| let x = 0; | |
| const interval = setInterval(() => { | |
| if (x === 5) { | |
| observer.error('Oops'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Observable } from 'rxjs'; | |
| import { retry } from 'rxjs/operators'; | |
| const observable = new Observable<number>(observer => { | |
| let x = 0; | |
| const interval = setInterval(() => { | |
| if (x === 5 && Math.random() < .5) { | |
| observer.error('Oops'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Observable, throwError } from 'rxjs'; | |
| import { catchError } from 'rxjs/operators'; | |
| observable | |
| .pipe( | |
| catchError(err => { | |
| console.warn('I want to display this error globally'); | |
| return throwError(err); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Observable, of } from 'rxjs'; | |
| import { catchError } from 'rxjs/operators'; | |
| observable | |
| .pipe( | |
| catchError(err => of(-1)) | |
| ) | |
| .subscribe({ | |
| next: x => console.log(`${x} received`), | |
| complete: () => console.log('Completed'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| observable.subscribe({ | |
| next: x => console.log(`${x} received`), | |
| complete: () => console.log('Completed'), | |
| error: err => console.warn(err) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Observable } from 'rxjs'; | |
| const observable = new Observable<number>(observer => { | |
| let x = 0; | |
| const interval = setInterval(() => { | |
| if (x === 5) { | |
| observer.error('Oops'); | |
| } | |
| else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { interval } from 'rxjs'; | |
| import { mergeMap, takeUntil } from 'rxjs/operators'; | |
| interval(1000) | |
| .pipe( | |
| takeUntil(this.destroy$), | |
| mergeMap(() => this._http.put('http://localhost:3000/documents/1', { lastUpdate: new Date() })) | |
| ) | |
| .subscribe(x => console.log('Document saved!')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { interval } from 'rxjs'; | |
| import { mergeMap, take, tap } from 'rxjs/operators'; | |
| interval(1000) | |
| .pipe( | |
| take(3), | |
| mergeMap(() => interval(1000).pipe(tap(() => console.log('foo')))) | |
| ) | |
| .subscribe(() => console.log('bar')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { interval } from 'rxjs'; | |
| import { mergeMap, tap } from 'rxjs/operators'; | |
| const s = interval(1000) | |
| .pipe( | |
| mergeMap(() => interval(1000).pipe(tap(() => console.log('foo')))) | |
| ) | |
| .subscribe(() => console.log('bar')); | |
| setTimeout(() => s.unsubscribe(), 3500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const s = interval(1000) | |
| .pipe( | |
| map(() => interval(1000).pipe(tap(() => console.log('foo'))).subscribe()) | |
| ) | |
| .subscribe(() => console.log('bar')); | |
| setTimeout(() => s.unsubscribe(), 3500); |