簡體   English   中英

RXJS Angular-如何在包含可觀察對象的foreach循環結束時調用函數?

[英]RXJS Angular - How to call a function at end of foreach loop that contains observables?

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結束

檢查以下代碼。

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);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM