【问题标题】:Angular/RxJS - Converting a promise and inner observable to one single observable to be returnedAngular/RxJS - 将一个 promise 和内部 observable 转换为一个要返回的 observable
【发布时间】:2020-08-04 14:53:08
【问题描述】:

我有一个承诺,一旦它实现,就会创建并订阅一个 observable。我需要做的是将这个 Promise 转换为一个 observable 并返回这个 observable 以便在其他地方订阅(这将包括内部 observable)。我使用from 将promise 转换为可观察对象,然后使用concatMap 链接内部可观察对象。但是,在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);
          }
        })
      );
    })
  );
}

【问题讨论】:

    标签: javascript angular typescript rxjs


    【解决方案1】:

    我认为您只需要返回您在 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);
              }
            })
          );
        })
      );
    }
    

    否则,它看起来不错!

    【讨论】:

    • 啊!!不敢相信我错过了。非常感谢!!
    猜你喜欢
    • 2016-12-25
    • 2018-02-19
    • 2021-12-20
    • 1970-01-01
    • 2018-06-20
    • 2020-12-01
    • 2019-12-25
    • 2021-05-24
    • 2016-08-15
    相关资源
    最近更新 更多