Learn RxJS
Search…
timeout

signature: timeout(due: number, scheduler: Scheduler): Observable

Error if no value is emitted before specified duration

Examples

Example 1: Timeout after 2.5 seconds
1
// RxJS v6+
2
import { of } from 'rxjs';
3
import { concatMap, timeout, catchError, delay } from 'rxjs/operators';
4
5
// simulate request
6
function makeRequest(timeToDelay) {
7
return of('Request Complete!').pipe(delay(timeToDelay));
8
}
9
10
of(4000, 3000, 2000)
11
.pipe(
12
concatMap(duration =>
13
makeRequest(duration).pipe(
14
timeout(2500),
15
catchError(error => of(`Request timed out after: ${duration}`))
16
)
17
)
18
)
19
/*
20
* "Request timed out after: 4000"
21
* "Request timed out after: 3000"
22
* "Request Complete!"
23
*/
24
.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 1yr ago