68

Previously in rxjs4 there was a method in the BehaviorSubject called: getValue() (doc here).

This method does not exist anymore in rxjs5.

So the only solution that I found to get the value of a BehaviorSubject was:

let value;
myBehaviorSubject.take(1).subscribe( (e) => value = e );

This code runs synchronously (I do not exactly understand why, but it does ...) and gets the value. It works, but it's not as clean as it could be if getValue() was present:

let value = myBehaviorSubject.getValue();

Why getValue() was removed in rxjs5 and what's the cleanest solution to this problem?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Clement
  • 3,860
  • 4
  • 24
  • 36
  • 41
    The `BehaviorSubject` interface has been simplified - the getter is called just `.value`. – artur grzesiak Aug 05 '16 at 11:44
  • @arturgrzesiak I do not find any reference to this .value in the current doc: http://reactivex.io/rxjs/class/es6/BehaviorSubject.js~BehaviorSubject.html – Clement Aug 05 '16 at 12:24
  • 3
    docs may be not up to date, but still it works. Take a look here as well: http://reactivex.io/rxjs/test-file/spec-js/subjects/BehaviorSubject-spec.js.html#lineNumber47 – artur grzesiak Aug 05 '16 at 12:49
  • Thanks for pointing this ! – Clement Aug 05 '16 at 13:39
  • @Clement was correct, this was never added to the documentation because it was never intended for consumer use. Anyone who upgrades to 6.5.0 will find that it is no longer working as it has been removed: https://github.com/ReactiveX/rxjs/issues/5085 – Jamie Barker Nov 01 '19 at 09:55
  • @JamieBarker this is incorrect, the discussion you linked is about `of` and not `BehaviorSubject` – Alfred Aug 21 '20 at 07:26

2 Answers2

105

As was pointed out by artur grzesiak in the comments, the BehaviorSubject interface was cleaned up, and the getter is now just myBehaviorSubject.value.

I just wanted to add this as an answer because I almost didn't read the comments to the original question, and would have missed the correct answer.

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
Tyson Phalp
  • 2,737
  • 3
  • 20
  • 12
  • 3
    "*I just wanted to add this as an answer because I almost didn't read the comments to the original question, and would have missed the correct answer.*" If the concern is that you're repeating the comment and [it's not "your" answer](https://meta.stackoverflow.com/a/343089/1028230), you can always [community wiki it](https://meta.stackexchange.com/a/11741/184684) (though at 31 votes, I understand if the altruism wanes). – ruffin Sep 06 '18 at 19:47
  • 8
    Seems to me if anyone isn't understanding how answers work it's Artur and not Tyson – Simon_Weaver Sep 11 '18 at 08:45
  • 6
    That comment links to a breaking change about of, it has nothing to do with behavior subjects. – Adrian Brand Nov 06 '19 at 06:54
  • 6
    As of RxJS 6.5.4 (and looking forward to 7.x) both `.value` and `.getValue()` will be supported on **BehaviorSubjects** as a way to access the current value https://github.com/ReactiveX/rxjs/blob/6.x/src/internal/BehaviorSubject.ts – Birches Feb 18 '20 at 16:42
  • 1
    I submitted an edit to remove the comment about `.value` of `BehaviorSubject` being deprecated. As @AdrianBrand stated the github discussion is about `of` and not `BehaviorSubject`. `.value` **is** and **will continue being** supported in `BehaviorSubject` – Alfred Aug 21 '20 at 07:20
8

Look at the source code to a behavior subject

https://github.com/ReactiveX/rxjs/blob/master/src/internal/BehaviorSubject.ts

It still has a getValue method, it has a value property that just calls getValue, it was there in RxJs5.

Here is a StackBlitz using RxJs5.

https://stackblitz.com/edit/typescript-gcbif4

All the comments talking about a breaking change in 6.5.0 are linking to comments about observables make with of not behavior subjects.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60