You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be great if we can get the last value from an Observable. Since toPromise is deprecated and the replacements firstValueFrom and lastValueFrom are also not the right one we want. Because firstValueFrom resolves on first value emitted by next. And lastValueFrom 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 then T | 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 and firstValueFrom 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 a BehaviorSubject 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) because Observable has not value. But I only have access to the Observable. We could find a way... Maybe I have to write my own wrapper.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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