2

The behavior of delay has changed in RxJs 7. While I understand the reasoning for the change, it was useful for a demo project that I use to simulate over-the-wire API delays. In RxJs 6, the code below would only log to the console after the 5 second delay, but in 7 it is immediately logged (7 no longer waits for delays on an empty observable). Is there a way to replicate the following in RxJs 7?

import { EMPTY } from 'rxjs';
import { delay } from 'rxjs/operators';

EMPTY.pipe(delay(5000)).subscribe({
  complete: () => {
    console.log('complete');
  },
});

See the Stackblitz examples below.

RxJs 7 (no delay): https://stackblitz.com/edit/rxjs-yx19nb?file=index.ts RxJS 6 (5 second delay): https://stackblitz.com/edit/rxjs-8rmhov?file=index.ts

Jim Cooper
  • 5,113
  • 5
  • 30
  • 35
  • Why it has to be EMPTY? can you use of(null) instead as the source stream ? – Fan Cheung Nov 18 '21 at 02:15
  • 1
    This bug occurred only when the source Observable didn't emit any `next` and just completed. I remember that because I fixed it myself :) https://github.com/reactivex/rxjs/issues/4249 and PR https://github.com/ReactiveX/rxjs/pull/4444 – martin Nov 18 '21 at 08:56
  • `EMPTY.pipe(delay(5000))` emitting after 5 secs sounds like a bug to me too. When an observable emits a `complete` notification I expect it to complete immediately and honor only specific operators like `finalize`. Delaying a `complete` might lead to unintended consequences. As for your prototyping scenario, I'd highly suggest using `timer(5000)` or something like `of().pipe(delay(5000))` instead. – ruth Nov 18 '21 at 09:29

1 Answers1

2

Just use a timer instead.

timer(5000).pipe(
  ignoreElements()
).subscribe(...);
Mrk Sef
  • 7,557
  • 1
  • 9
  • 21