You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
number specifying period within which Observable must emit values
Date specifying before when Observable should complete
import { fromEvent } from 'rxjs';
import { timeout } from 'rxjs/oeprators';
fromEvent(document, 'click').pipe(
timeout(2100)
// a bit bigger timespan
// as interval created with `interval` is non-deterministic
// and might fire a bit later than scheduled
).subscribe(console.log);
TIME
Im
1s
2s
3s
FINAL
TimeoutError
TIME
Im
1s
2s
3s
4s
5s
6s
7s
8s
FINAL
C
C
C
C
TimeoutError
timeoutWith(due, fallbackObservable)
import { fromEvent, interval } from 'rxjs';
import { timeoutWith, take } from 'rxjs/operators';
const click$ = fromEvent(document, 'click');
const intv$ = interval(1000).pipe(take(5));
click$.pipe(
timeoutWith(2000, intv$)
).subscribe(console.log);
TIME
Im
1s
2s
3s
4s
5s
6s
7s
8s
9s
10s
11s
12s
click
C
C
FINAL
Ev
Ev
0
1
2
3
4
observeOn(scheduler, delay?)
import { interval, animationFrameScheduler } from 'rxjs';
import { observeOn } from 'rxjs/operators';
const interval$ = interval(10);
// interval is scheduled with async scheduler by default
interval$.pipe(
observeOn(animationFrameScheduler)
).subscribe(val => {
someDiv.style.marginLeft = val + 'px';
});
subscribeOn(scheduler, delay?)
import { of, merge, asyncScheduler } from 'rxjs';
import { subscribeOn } from 'rxjs/operators';
const a = of(1, 2, 3, 4).pipe(
subscribeOn(asyncScheduler)
);
const b = of(5, 6, 7, 8);
merge(a, b).subscribe(console.log);
OPERATION
obs. b
other ops in the event loop
obs. a
FINAL
5, 6, 7, 8
1, 2, 3, 4
toArray()
import { interval } from 'rxjs';
import { toArray, take } from 'rxjs/operators';
interval(1000).pipe(
take(4),
toArray()
).subscribe(console.log);
TIME
Im
1s
2s
3s
4s
FINAL
[0, 1, 2, 3]
When the source Observable throws error, no array will be emitted.
If take(4) would be placed after toArray(), we would get infinite Observable and no results.