Home » RxJS timeout() Utility Operator

RxJS timeout() Utility Operator

by Online Tutorials Library

RxJS timeout() Utility Operator

The RxJS timeout() operator is a utility operator that allows us to abort the observable with an onError termination if that Observable fails to emit any items during a specified span of time.

In other words, we can say that the RxJS timeout() operator throws an error if the source observable does not emit a value after the given timeout or within a specified duration.

Syntax:

Following is the syntax of the RxJS timeout() utility operator:

Or

Parameter Explanation

  • timeout: This is an input that can be of type number or date. The value from the source Observable must be emitted.
  • due: This is a number which specifies the period within which observable must emit values or date specifying before when observable should be completed.
  • scheduler: This is used for controlling when timeout checks occur. This is an optional argument. Its default value is async.

Return value

The RxJS timeout() operator returns an observable that mirrors the source’s behavior unless timeout checks fail.

Let us see some examples of the RxJS timeout() operator to understand it clearly.

Example 1 (Use Date to check if Observable is completed)

Output:

After executing the above example, you will see the following result:

RxJS timeout() Utility Operator

In the above example, you can see that the observable interval is going on and the timeout is given as new Date (“November 09, 2020 19:10:45”). When the timeout is occurred, it has thrown an error.

Example 2 (Timeout after 2.5 seconds)

Output:

After executing the above example, you will see the following result:

RxJS timeout() Utility Operator

In the above example, you can see that we have set timeout duration for 2.5 second and when the timeout is occurred, it throws an error.

Example 3 (Check if ticks are emitted within a certain timespan)

Output:

After executing the above example, you will see the following result:

RxJS timeout() Utility Operator


Next TopicRxJS Operators

You may also like