简体   繁体   English

如何在 Angular 中的 map function 中添加订阅

[英]How to add subscribe inside a map function in Angular

I am running into an issue where I would like to modify the objects inside an array based on data returned from a subscription.我遇到了一个问题,我想根据订阅返回的数据修改数组中的对象。

this.options = this.options.map(data => {
      this.workflowTemplateService.getWorkflowTemplateStatistics(data.id).subscribe(res => {
        const available = (res.contactsInProgress + res.contactsInWaitingRoom + res.pausedCount).toString();
        const waitingRoomLimit = res.waitingRoomLimit;
        const quota = available.toString() + '/' + waitingRoomLimit.toString();
        console.log("Hit quota: ", quota);
        return {...data, quota};
      });
});

I wanted to do something like this where I would be able to add the quota property to each of the objects inside the array but I am getting an error related to returning void.我想做这样的事情,我可以将配额属性添加到数组中的每个对象,但我收到与返回 void 相关的错误。 I was wondering if there were another way where I would able to modify the options such that I can add the quota property into the options array.我想知道是否有另一种方法可以修改选项,以便可以将配额属性添加到选项数组中。

First, you should convert your observable to promise to be able to take the advantage of using async/await.首先,您应该将您的 observable 转换为 promise 以便能够利用使用 async/await。 You can convert your observable to promise by using firstValueFrom operator in rxjs to take the first value emitted and close the subscription after that.您可以使用 rxjs 中的firstValueFrom运算符将您的 observable 转换为 promise 以获取发出的第一个值并在此之后关闭订阅。

const promise = firstValueFrom(this.workflowTemplateService.getWorkflowTemplateStatistics(data.id));

By converting your observable to a promise, now you can mark the map callback function as async .通过将您的 observable 转换为 promise,现在您可以将map回调 function 标记为async Here is the link about async map callback function:这是关于异步 map 回调 function 的链接:

Best way to call an asynchronous function within map? 在 map 中调用异步 function 的最佳方法?

and your result would be something like below你的结果将如下所示

async updateOptions() {
   this.options = await Promise.all(
      this.options.map(async data => {
      const result = await firstValueFrom(this.workflowTemplateService.getWorkflowTemplateStatistics(data.id));
      ...
      return {...data, quota};
      })
   )
}
this.options$ = of(this.options)
.pipe(
  switchMap(data => this.workflowTemplateService.getWorkflowTemplateStatistics(data.id)),
  map(res => {
    const available = (res.contactsInProgress + res.contactsInWaitingRoom + res.pausedCount).toString();
        const waitingRoomLimit = res.waitingRoomLimit;
        const quota = available.toString() + '/' + waitingRoomLimit.toString();
        console.log("Hit quota: ", quota);
        return {...data, quota};
  })
)

Now you can suscribe directly to this.options$ which is now an observable or you can used directly in the template like options$ | async现在您可以直接订阅 this.options$,它现在是一个可观察对象,或者您可以直接在模板中使用,例如options$ | async options$ | async

this.options$.suscribe(res => console.log(res))

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

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