In my service, I have defined some method which will use Http.
For example:
getUsers(){
this._http.get('someapi')
.map(res=>res.json())
.subscribe(success=>this.users = success; console.log(success))
}
What I am planing to do, is to set up this method so that it returns a promise, for example:
this.getUsers().subscribe(success=> DO SOMETHING);
or
this.getUsers().then(DO SOMETHING);
So far I got this:
getUsers(){
var getUsersStream = this._http.get('someapi')
.map(res=>res.json())
.subscribe(success=>this.users = success ;console.log('DONE'))
return Observable.of(getUsersStream)
}
Which indeed allows me to do:
this.getUsers().subscribe(success=> console.log('after success'))
however, when I debug, I can see that console output is:
after success
DONE
Which doesnt really works like it supposed to. Where am I doing mistake?
UPDATE
Even though I the answer provided by Thierry works, I guess I will explain my question in bit more detail.
I have method getUserById() in my service, which does the following:
getUserById(someId){
if(this.users.length){
var i = _.findIndex(this.users, {"id":someId};
return this.users[i]
} else {
this.getUsers().then(find the user)//then should be invoked when users are there
}
}