首页 > 解决方案 > 如何订阅 flatMap 的结果?

问题描述

如何订阅 flatMap 的结果?

   timer(0, 2000)
      .pipe(
        flatMap(() => this.scannerService.scan(this.scanType)),
        takeWhile(res => res.errorDesc !== "SUCCESS")
      )
      .subscribe((res: IScanResponse) => {
});

我试过在.subscribe()之后使用pipe。但我得到错误:

您在预期流的位置提供了无效对象。您可以提供 Observable、Promise、Array 或 Iterable。

我的远程功能是:

public scan(scanType: string): Observable<any> {}

我的进口是:

import { timer } from "rxjs";
import { takeWhile } from "rxjs/operators";
import { ScannerService } from "src/app/api/ScannerService";
import { flatMap } from "rxjs/operators";

标签: angularrxjsrxjs6

解决方案


After the .pipe() since .pipe() returns an observable

const tm = timer(0, 2000).pipe(
     flatMap(() => this.scannerService.scan(this.scanType)),
     takeWhile(res => res)
).subscribe(res=> {
//your code
});;

推荐阅读