簡體   English   中英

Angular/RxJS - 將 promise 和內部可觀察對象轉換為要返回的單個可觀察對象

[英]Angular/RxJS - Converting a promise and inner observable to one single observable to be returned

我有一個 promise,一旦它完成,就會創建並訂閱一個 observable。 我需要做的是將此 promise 轉換為可觀察對象,並將此可觀察對象返回以在其他地方訂閱(包括內部可觀察對象)。 我使用from將 promise 轉換為可觀察對象,然后使用concatMap鏈接內部可觀察對象。 但是,在concatMap的開頭,我的代碼中出現了我試圖從初始 promise 檢索的xfdfString字符串變量的錯誤。 也許我的新函數的內部可觀察對象實際上並沒有返回可觀察對象本身? 我已經嘗試了一些方法來解決這個問題,但沒有運氣,所以任何想法都會非常感激。

錯誤:

Argument of type '(xfdfString: string) => void' is not assignable to parameter of type '(value: string, index: number) => ObservableInput<any>'.
  Type 'void' is not assignable to type 'ObservableInput<any>'.ts(2345)

原裝 function:

  save() {
    const { annotManager } = this.wvInstance;
    annotManager.exportAnnotations({ links: false, widgets: false }).then(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      const data: SignDocument = { encodedDocument: encodeBase64(xfdfString) };

      this.documentService.signDocument(requestId, data)
        .pipe(
          switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
          switchMap(nextTask => {
            if (!nextTask) {
              return this.workflowService.completeFolder();
            } else {
              return observableOf(nextTask);
            }
          }),
        ).subscribe(response => console.log(response));
    });
  }

我嘗試使用更高階的 observable 來代替返回一個 observable:

save(): Observable<any> {
  const { annotManager } = this.wvInstance;

  const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));

  return docViewerobservable.pipe(
    concatMap(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      let data = { encodedDocument: encodeBase64(xfdfString) };
      

      this.documentService.signDocument(requestId, data)
      .pipe(
        switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
        switchMap(nextTask => {
          if (!nextTask) {
            return this.workflowService.completeFolder();
          } else {
            return observableOf(nextTask);
          }
        })
      );
    })
  );
}

我認為您只需要返回您在concatMap中創建的 Observable ;-)

save(): Observable<any> {
  const { annotManager } = this.wvInstance;

  const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));

  return docViewerobservable.pipe(
    concatMap(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      let data = { encodedDocument: encodeBase64(xfdfString) };
      

      return this.documentService.signDocument(requestId, data)
      .pipe(
        switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
        switchMap(nextTask => {
          if (!nextTask) {
            return this.workflowService.completeFolder();
          } else {
            return observableOf(nextTask);
          }
        })
      );
    })
  );
}

否則,它看起來不錯!

暫無
暫無

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

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