I am trying to turn the observable for params that I get off of the ActivatedRoute
into a promise but haven't had any luck. I have been able to turn http
requests into promises successfully:
this.riaService.getAdvisorsForState(this.activeState)
.then(rias => {
this.riasForState = rias.json();
console.log(this.riasForState);
});
// this all works ^
But I have not been able to turn the 'activatedRoute.params' into a promise:
export class InvestmentAdvisorStateComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute, private riaService: InvestmentAdvisorService) { }
getStateFromUrl() {
this.activatedRoute.params
.toPromise()
.then(params => {
console.log('HERE',params)
});
}
// this does not work for me ^
This is what I have right now:
getStateFromUrl() {
this.activatedRoute.params
.subscribe((param: any) => {
this.activeState = param['state'];
});
}
// works ^
I am hoping to implement this as a promise thought so I can .then
off of it. Any help on how to do this is greatly appreciated.
I am trying to turn the observable for params that I get off of the ActivatedRoute
into a promise but haven't had any luck. I have been able to turn http
requests into promises successfully:
this.riaService.getAdvisorsForState(this.activeState)
.then(rias => {
this.riasForState = rias.json();
console.log(this.riasForState);
});
// this all works ^
But I have not been able to turn the 'activatedRoute.params' into a promise:
export class InvestmentAdvisorStateComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute, private riaService: InvestmentAdvisorService) { }
getStateFromUrl() {
this.activatedRoute.params
.toPromise()
.then(params => {
console.log('HERE',params)
});
}
// this does not work for me ^
This is what I have right now:
getStateFromUrl() {
this.activatedRoute.params
.subscribe((param: any) => {
this.activeState = param['state'];
});
}
// works ^
I am hoping to implement this as a promise thought so I can .then
off of it. Any help on how to do this is greatly appreciated.
-
What behavior do you expect? This promise probably only pletes when you navigate away from that route while
http.get()
pletes when the response from the server arrives. – Günter Zöchbauer Commented Oct 6, 2016 at 17:29 -
@GünterZöchbauer Oh, I didnt realize that. I edited my post to show the functionality that I have right now with
.subscribe
. I am trying to turn my observable into a promise so I can chain it with.then
– georgej Commented Oct 6, 2016 at 17:35 -
Why not just with
.map()
– Günter Zöchbauer Commented Oct 6, 2016 at 17:50 - hmm.. that didnt allow me to turn it into a promise – georgej Commented Oct 6, 2016 at 20:33
- If you turn it into a promise, you defeat the purpose of using the "live" route. You might as well just use the snapshot. – Paul Samsotha Commented Oct 7, 2016 at 1:39
1 Answer
Reset to default 9The main difference between observable and promise is, that observable can emit multiple events while a promise only emits a single event.
To make your example work, I assume you are only interested in the first event.
getStateFromUrl() {
this.activatedRoute.params
.first()
.toPromise()
.then(params => {
console.log('HERE',params)
});
}
This way the promise created with toPromise()
is pleted with the first params
event.