Reputation: 11
I am working on an angular 8 project and I need to return a promise inside a subscribe. Here is my observable:
export async function resolvePage(api: ApiService, transition: Transition) {
const slug = transition.params().slug.split('/').slice(-1).pop();
return api.get(`/pages/${slug}`).toPromise();
}
And I need to do something like this, but it doesn't work:
export async function resolvePage(
api: ApiService,
transition: Transition,
stateService: StateService
) {
const slug = transition
.params()
.slug.split("/")
.slice(-1)
.pop();
return api.get(`/pages/${slug}`).subscribe(
(res: any) => {
return new Promise((resolve, reject) => {
resolve(res);
});
},
() => {
stateService.go("app.404");
}
);
}
How can I do it?
Upvotes: 0
Views: 355
Reputation: 126
You can use Observable.toPromise()
method. See https://www.learnrxjs.io/operators/utility/topromise.html
Upvotes: 1