maybesomename
maybesomename

Reputation: 121

In rxjs how can I set a minimal time before a single value is emitted?

I have an http request as an observable, but in parallel I play some animation that takes 2 seconds. I want the animation to finish before the request is completed, but I want the request to take the least possible time after the 2 seconds. So if the request in itself takes 1.5 seconds, I want the value to be emitted in 2 seconds, if the request takes 3 seconds, I want the value to be emitted in 3 seconds. Thus,

request.pipe(delay(2000))

wont do, since it simply adds to the time of the request. My first solution is

forkJoin([request,timer(2000)]).pipe(val=>val[0])

but I hoped there is an operator for this behavior, and I couldn't find it.

Upvotes: 2

Views: 811

Answers (1)

Mrk Sef
Mrk Sef

Reputation: 8022

For your case, forkJoin works just fine. There's a solution that's slightly more flexible.

You can use combineLatest to accomplish this in the exact same way, which doesn't rely on your request completing.

combineLatest([
  request,
  timer(2000)
]).pipe(
  map(v => v[0])
)

Upvotes: 6

Related Questions