Get the latest value from Observable without subscribe. #7123
infacto
started this conversation in
Ideas / Feature request
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It would be great if we can get the last value from an Observable. Since
toPromise
is deprecated and the replacementsfirstValueFrom
andlastValueFrom
are also not the right one we want. BecausefirstValueFrom
resolves on first value emitted by next. AndlastValueFrom
requires the Observable to being completed. But in our case we just want the latest received value from Observable. Regardless of any value is received yet or if the Observable is completed or not.The new feature I suggest is a property or method like
getLastValue()
which simply returns the latest available value sync (no Promise). The return type is thenT | undefined
. Undefined if no value received yet (or received undefined). The type is nullish now. But this is ok and expected.Use case: Get information about the current state of variables which are Observables. When it doesn't matter if any value is emitted or completed.
Or how to achieve this / solve my issue? The function
lastValueFrom
requires Observable to complete andfirstValueFrom
returns only the first value. I just want the current (last received value). Currently I have to subscribe to it, take the last, and unsubscribe. Write some ugly code. Any idea is welcome. But what do you think about this suggestion? In my case I have aBehaviorSubject
which has an init value and is returned outside as Observable. In this case the strict type is not nullish. But I don't care about. I can handle and expect nullish here. I think it's not the worst idea and is a welcome way to quickly get the current value is a pretty way in this use case.Or we just extend the method
lastValueFrom
to get the last value without complete Observable. I want a method what will immediately return a value and not wait for a value / complete and the code never returns. Maybe this will only work with e.g.BehaviorSubject
(getValue
) becauseObservable
has not value. But I only have access to theObservable
. We could find a way... Maybe I have to write my own wrapper.Beta Was this translation helpful? Give feedback.
All reactions