3

Having such a convenient method like .startWith it would make sense to have his oposite, .endWith, which makes the observable yield a value whenever it gets completed.

I have come up with this solution, but is there anything better? This thing gets a bit hard to read for what it is.

source.concat(Rx.Observable.just(lastValue))
olivarra1
  • 3,269
  • 3
  • 23
  • 34
  • 1
    You're doing it the right way. There's no `endWith` because `cocnat` is exactly what it does already. Note that there's also `defaultIfEmpty` with a similar purpose. – martin Mar 22 '17 at 15:05
  • @martin Actually, for my specific case `defaultIfEmpty` is enough. Haven't noticed that operator, thank you! – olivarra1 Mar 22 '17 at 16:17

1 Answers1

0

There is in RxJS6 (no clue when it was added to be honest)

Documentation:

https://rxjs-dev.firebaseapp.com/api/operators/endWith

Source: https://github.com/ReactiveX/rxjs/blob/df0ea7c78767c07a6ed839608af5a7bb4cefbde5/src/internal/operators/endWith.ts

Also, defaultIfEmpty() only emits a value if the observable CLOSES without emitting a value. It's a subtle, yet not so subtle distinction. It may have the same effect as endWith() in limited situations.

Example of endWith():

const source = of(1, 2, 3, 4, 5);
const example = source.pipe(
                            takeWhile(val => val != 4), 
                            endWith(4));

Emits:

[1, 2, 3, 4]

Also I'm noticing that the https://learnrxjs.io website is increasingly out of date, and currently doesn't show this operator.

Why did I need it?

I was looking for the ability to emit false until a condition became true, but never go back to false. So slightly similar to debouncing, but not quite.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689