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
?可能不是最干净的,但我认为它可以工作。这也可以帮助 waiting observable subscribe inside foreach to end
关于javascript - RXJS Angular - 如何在包含可观察对象的 foreach 循环结束时调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088419/