24 Jul 2024
1 min

delay – RxJS Reference

delay

delay time shifts each emitted value from the source observable by a defined time span or until a given date. If the delay argument is a Number, this operator time shifts the source observable by that amount of time expressed in milliseconds. If the delay argument is a Date, delay starts emitting all incoming values until that point at the date specified preserving the intervals between the values.

Note that delay preserves the relative time intervals between the values. It means that if the source observable emits 3 items synchronously, all 3 of them will be delayed by a certain amount of time but delivered synchronously with a minimum time span between them. The time span will be equal or very close to the time span between the values emitted by the source obsevable. This is in contrast to how interval operator works, which inserts delays in between the values.

delay operator works in the following way:

  1. Subscribe to a source observable
  2. When a new value arrives from a source observable, add the value to internal cache and start a delay interval for that value
  3. Once the interval elapses, send the corresponding value to the observer
  4. Once the source observable completes, send the complete notification to the observer
  5. If the source observable throws an error, send the error notification to the observer

By default, the operator uses setInterval through AsyncScheduler under the hood for scheduling.

Usage

delay is commonly used to mock some asynchronous activity, such as network requests.

Here’s an example that demonstrates how to simulate network latency with 3 requests:

const api = 'https://reqres.in/api/users/';
const urls = [1, 2, 3].map(id => api + id);

from(urls).pipe(
   mergeMap(url => mockHTTPRequest(url))
).subscribe(val => console.log(val));

function mockHTTPRequest(url) {
   return of(`Response from ${url}`).pipe(
       // responses come in a random order
       delay(Math.random() * 1000)
   );
}

Playground

Additional resources

See also

Frequently asked questions

How do I delay emitted values in RxJS?

Use the delay() operator in a pipe(), passing either a number of milliseconds or a Date. With a number, delay(500) shifts each emitted value from the source observable by 500 milliseconds. With a Date, the operator holds all incoming values until that specific point in time, preserving the intervals between them.

What is the difference between delay() and interval() in RxJS?

The delay() operator preserves the relative time intervals between source values, so values emitted synchronously are delivered synchronously after the delay, only with a minimum time span equal or close to the original spacing. In contrast, interval() inserts new delays between values, emitting them at a fixed cadence regardless of how the source emitted them.

How do I simulate network latency with delay() in RxJS?

Wrap a mock response in of() and pipe it through delay() with a time value, such as delay(Math.random() * 1000). Combine it with mergeMap() when handling several requests, so responses arrive in a random order. This approach is commonly used to mock asynchronous activity like HTTP calls during development or testing.

Which scheduler does the RxJS delay() operator use by default?

By default, delay() uses setInterval through the AsyncScheduler to schedule emissions. The operator caches each incoming value, starts a delay for it, and sends it to the observer once the interval elapses. When the source completes, it sends the complete notification, and if the source errors, it forwards the error immediately.

What is the difference between delay() and delayWhen() in RxJS?

Use delay() when the delay is a fixed number of milliseconds or a specific Date. The delayWhen() operator is the alternative when each value needs its own dynamically computed delay, so you should consult it when the delay depends on the emitted value itself rather than a constant.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.