0

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

1

You can use Observable.toPromise() method. See https://www.learnrxjs.io/operators/utility/topromise.html

maury844
  • 1,210
  • 15
  • 25