Learn RxJS
Search…
timeoutWith

signature: timeoutWith(due: number | Date, withObservable: ObservableInput, scheduler: SchedulerLike = async): OperatorFunction

Subscribe to second Observable if no emission occurs in given time span.

Examples

Example 1: Timeout after 1 second
1
// RxJS v6+
2
import { of } from 'rxjs';
3
import { timeoutWith, delay, concatMap } from 'rxjs/operators';
4
5
const fakeRequest = delayTime => of('!response!').pipe(delay(delayTime));
6
const requestTimeoutLogger = of('logging request timeout');
7
const timeoutThreshold = 1000;
8
9
of(timeoutThreshold + 1, timeoutThreshold - 1, timeoutThreshold + 3)
10
.pipe(
11
concatMap(e =>
12
fakeRequest(e).pipe(timeoutWith(timeoutThreshold, requestTimeoutLogger))
13
)
14
)
15
.subscribe(console.log);
16
17
/*
18
OUTPUT:
19
logging request timeout
20
!response!
21
logging request timeout
22
*/
Copied!

Additional Resources

Last modified 1yr ago