I dont know how to extract value from Observable to be returned by function in which Observable is present. I need just a value from it to be returned, nothing else.
Current version which works
function getValueFromObservable() {
this.store.subscribe(
(data:any) => {
console.log(data)
}
)
}
getValueFromObservable()
I need this to work, function to return value, and then:
function getValueFromObservable() {
this.store.subscribe(
(data:any) => {
return data
}
)
}
console.log(getValueFromObservable())
What am I doing wrong here?
For example this is my html template:
This is the field that binded with template from my Component:
I fetch genres of films from server dynamically. In order do communicate with server I have created
FilmService
This is the method which communicate server:
Why this method returns
Observable<Genre[]>
not something likeGenre[]
?JavaScript is
async
and it does not wait for a method to return value after an expensive process. With expensive I mean a process that take a time to return value. Like fetching data from server. So you have to return reference of Observable and subscribe it.For example in my Component :
Looks like you are looking for a "current value" getter inside an observable, when it emits and after an emission.
Subject
andObservable
doesn't have such a thing. When a value is emitted, it is passed to its subscribers and theObservable
is done with it.You may use
BehaviorSubject
which stores the last emitted value and emits it immediately to new subscribers.It also has a
getValue()
method to get the current value;Further Reading:
RxJS BehaviorSubject
How to get current value of RxJS Subject or Observable?
Observable values can be retrieved from any locations. The source sequence is first pushed onto a special observer that is able to emit elsewhere. This is achieved with the Subject class from the Reactive Extensions (RxJS).
Store value onto the observer.
To retrieve the value from elsewhere, subscribe to the observer like so:
Subjects are both Observables and Observers. There are other Subject types such as BehaviourSubject and ReplaySubject for other usage scenarios.
Don't forget to import RxJS.
If you want to pre-subscribe to the same Observable which will be returned, just use
This is not exactly correct idea of using
Observable
In the component you have to declare class member which will hold an object (something you are going to use in your component)
Then a
Service
will be returning you anObservable
:Component
should prepare itself to be able to retrieve a value from it:You have to assign a value from an
Observable
to a variable:And your template will be consuming variable
name
:Another way of using
Observable
is throughasync
pipe http://briantroncone.com/?p=623Note: If it's not what you are asking, please update your question with more details
I realize that this Question was quite a while ago and you surely have a proper solution by now, but for anyone looking for this I would suggest solving it with a Promise to keep the async pattern. A more verbose version would be creating a new Promise:
On the receiving end you will then have "wait" for the promise to resolve with something like this:
A slimmer solution would be using RxJS' .toPromise() instead:
The receiving side stays the same as above of course. Hope that helps!