简体   繁体   English

Angular Typescript - 异步等待内部订阅

[英]Angular Typescript - Async await on internal subscription

I have a function like this;我有这样的功能;

openModalWhenRemoveItem() {
  callRemoveService();
  openMyModal();
}

callRemoveService() function has a subscribe and it's async function, the openMyModal() function must be invoked after callRemoteService() callRemoveService()函数有一个 subscribe 并且它是异步函数,必须在callRemoteService()之后调用openMyModal()函数

callRemoveService() {
  combineLatest(myAccountSelector$, myCompanySelector$).pipe(
    switchMap(res) => 
    this.myService.remove(res[0].id, res[1].id))
    .subscribe((res)=> console.log(res))
}

I need to create a function that has to wait the internal subscription in callRemoveService() before calling the openModal() .我需要创建一个必须等待内部认购的功能callRemoveService()调用之前openModal() I want to try with async await but I can't find a solution.我想尝试使用async await但我找不到解决方案。 I tried in this way:我这样试过:

async callRemoveService() {
  await combineLatest(myAccountSelector$, myCompanySelector$).pipe(
    switchMap(res) => 
    this.myService.remove(res[0].id, res[1].id))
    .subscribe((res)=> console.log(res))
}

openModalWhenRemoveItem() {
  callRemoveService().then(() => {openMyModal();});
  
}

but it doesn't work.但它不起作用。 I cant put openMyModal() function into subscribe.我无法将openMyModal()函数放入订阅中。

The solution in your case would be to return the Observable from the callRemoveService method instead of subscribing to it.您的情况的解决方案是从callRemoveService方法返回Observable而不是订阅它。 The caveat with this approach is that you need to call subscribe in all the places where you call callRemoveService .这种方法需要注意的是,你需要调用subscribe所有在您拨打的地方callRemoveService

If you do that, then it is trivial:如果你这样做,那么它是微不足道的:

callRemoveService(): Observable<any> {
  return combineLatest(myAccountSelector$, myCompanySelector$).pipe(
    switchMap(res) => 
    this.myService.remove(res[0].id, res[1].id));
}

Then your openModalWhenRemoveItem method becomes this:然后你的openModalWhenRemoveItem方法变成这样:

openModalWhenRemoveItem() {
  callRemoveService().subscribe(() => {
    openMyModal();
  };
}

combineLatest is a RxJS operator, it returns an Observable. combineLatest是一个 RxJS 操作符,它返回一个 Observable。 You can't simply await an Observable, it's not a Promise.你不能简单地等待一个 Observable,它不是一个 Promise。 Simply subscribe to it.只需订阅它。

callRemoveService(): Observable<any> {
  return combineLatest(myAccountSelector$, myCompanySelector$);
}

and then :进而 :

openModalWhenRemoveItem() {
  callRemoveService().subscribe(openMyModal);
}

Alternatively, you can transform your Observable to Promise using the lastValueFrom operator, and await it :或者,您可以使用lastValueFrom运算符将 Observable 转换为 Promise,并等待它:

callRemoveService(): Promise<any> {
  return lastValueFrom( combineLatest(myAccountSelector$, myCompanySelector$)) ;
}

async openModalWhenRemoveItem() {
  await callRemoveService();
  openMyModal();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM