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循环,然后在循环和订阅完成后调用我的函数?
答案 0 :(得分:0)
您是否想到过forkJoin
?可能不是最干净的,但我认为它可以工作。这也可以帮助waiting observable subscribe inside foreach to end
答案 1 :(得分:0)
检查以下代码。
const _subscriptions = [];
Observable.of(this.attachmentList)
.subscribe((attachments) => {
_subscriptions = attachments.map(element1 => {
this.service.getPageOfAttachment()
.flatMap(response => this.service.download((JSON.parse(response['Content']).mediaUrl)))
.subscribe(response => { },
error => (console.log(error)),
completed => (console.log("completed...!!!")));
});
});
Promise.all(_subscriptions)
.then(() => {
this.getPdfSharp(pdfSharpByteArray, attachment.entityName, i);
});