function deprecated operator
When the passed timespan elapses before the source emits any given value, it will unsubscribe from the source, and switch the subscription to another observable.
Replaced with timeout. Instead of timeoutWith(100, a$, scheduler), use timeout with the configuration object: timeout({ each: 100, with: () => a$, scheduler }). Instead of timeoutWith(someDate, a$, scheduler), use timeout with the configuration object: timeout({ first: someDate, with: () => a$, scheduler }). Will be removed in v8.
timeoutWith(dueBy: Date, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R> dueBy | Date | |
switchTo | ObservableInput<R> | |
scheduler | SchedulerLike | Optional. Default is |
OperatorFunction<T, T | R>
timeoutWith(waitFor: number, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R> waitFor | number | |
switchTo | ObservableInput<R> | |
scheduler | SchedulerLike | Optional. Default is |
OperatorFunction<T, T | R>
Used to switch to a different observable if your source is being slow.
Useful in cases where:
TimeoutError emitted by the default usage of timeout.If the first parameter is passed as Date and the time of the Date arrives before the first value arrives from the source, it will unsubscribe from the source and switch the subscription to another observable.
Use Date object to switch to a different observable if the first value doesn't arrive by a specific time.
Can be used to set a timeout only for the first value, however it's recommended to use the timeout operator with the first configuration to get the same effect.
Fallback to a faster observable
import { interval, timeoutWith } from 'rxjs';
const slow$ = interval(1000);
const faster$ = interval(500);
slow$
.pipe(timeoutWith(900, faster$))
.subscribe(console.log); Emit your own custom timeout error
import { interval, timeoutWith, throwError } from 'rxjs';
class CustomTimeoutError extends Error {
constructor() {
super('It was too slow');
this.name = 'CustomTimeoutError';
}
}
const slow$ = interval(1000);
slow$
.pipe(timeoutWith(900, throwError(() => new CustomTimeoutError())))
.subscribe({
error: err => console.error(err.message)
});
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/timeoutWith