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.
The 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 completed with the first params
event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With