javascript - RXJS Angular - 如何在包含可观察对象的 foreach 循环结束时调用函数?
问题描述
this.attachmentList.forEach((attachment, i) => {
this.service.getPageOfAttachment().flatMap(response =>
this.service.download((JSON.parse(response['Content']).mediaUrl)))
.subscribe(response => {
}, error => (console.log(error)),
() => {
if (i > this.attachmentList.length - 1) this.getPdfSharp(pdfSharpByteArray, attachment.entityName, i);
})
})
在 foreach 循环结束时,我正在检查“i”的值,如果它大于列表长度,则调用该函数。这样做的问题是,由于我在循环内订阅,所以 I 的值并不总是正确的顺序,并且该函数被提前调用。如何遍历包含订阅的 foreach 循环,然后在循环和订阅完成时调用我的函数?
解决方案
你想过forkJoin
吗?可能不是最干净的,但我认为它可以工作。这也可以帮助等待 foreach 内的可观察订阅结束
推荐阅读
- azure-devops - 缺少对 Azure DevOps 中的跨存储库策略或项目范围的分支策略的访问权限
- android - 如何在运行时有条件地添加任何特定的 React 包?
- npm - 在 Nexus 3.19.1-01 中使用 npm dist-tag 时出现错误请求(Http 400)
- css - 如何根据窗口分辨率显示更多列?
- sqlite - 如何从 SQLite 中的表中选择最新的 100 个不同条目?
- javascript - 如何减少 expo 项目中的 apk 大小?
- colors - (Python) Plotly Scatter - 用整数值指定线条颜色
- java - 借助 Spring 的 AbstractBeanDefinition 的反射进行序列化
- kubernetes-ingress - Nginix 在使用 -SSL Passthrough 时崩溃
- node.js - 密码加密nodeJS PBKDF2