首页 > 解决方案 > Angular/RxJS - 将一个 promise 和内部 observable 转换为一个要返回的 observable

问题描述

我有一个承诺,一旦它实现,就会创建并订阅一个 observable。我需要做的是将这个 Promise 转换为一个 observable 并返回这个 observable 以便在其他地方订阅(这将包括内部 observable)。我曾经from将 promise 转换为 observable,然后用于concatMap链接内部 observable。但是,在开始时concatMap,我的代码中出现了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)

原始功能:

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

标签: javascriptangulartypescriptrxjs

解决方案


我认为你只需要返回你在里面创建的 Observable concatMap;-)

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

否则,它看起来不错!


推荐阅读