在Rxjs 6中将concat与管道一起使用的正确方法是什么?

时间:2018-07-27 22:03:40

标签: angular rxjs angular-httpclient rxjs6

我有一个服务器呼叫,可能返回HTTP 202。受this SO线程的影响,我有以下内容:

this.http.get(url)
  .pipe(
    map(response => {
      if (response.status === 202) {
        throw response;
      }
      return response;
    }),
    retryWhen(errors => {
      return errors.pipe(
        delay(1000),
        take(3),
        concat(response => Observable.throw('Retries exceeded'))
      );
    }),
    catchError(handleError)
  );

在使用deprecated时收到concat警告。我了解新的concatrxjs中,而不在rxjs/operator中。

但是,在这里使用新的static concat运算符的正确方法是什么?

找到了以下from this site

import { concat } from 'rxjs/operators';
a$.pipe(concat(b$, c$));

// becomes

import { concat } from 'rxjs';
concat(a$, b$, c$);

我无法在示例代码中缠住Observable的串联对象吗?

更新1

将代码更改为:

return concat(this.http.get(url)
  .pipe(
    map(response => {
      if (response.status === 202) {
        throw response;
      }
      return response;
    }),
    retryWhen(errors => {
      return errors.pipe(
        delay(1000),
        take(3)
      );
    }),
    catchError(handleError)
  ), response => Observable.throw('Retries exceeded'));

但这会导致:

core.js:1598 ERROR TypeError: You provided 'function (response) { return rxjs__WEBPACK_IMPORTED_MODULE_2__["Observable"].throw('Retries exceeded'); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:41)
at subscribeToResult (subscribeToResult.js:6)
at 

1 个答案:

答案 0 :(得分:2)

可管运算符只是接受可观察值并返回可观察值的函数。因此,您可以像这样使用concat工厂函数:

retryWhen(errors => {
  return errors.pipe(
    delay(1000),
    take(3),
    o => concat(o, throwError('Retries exceeded'))
  );
})

此外,concat运算符将替换为/重命名为concatWith。参见this issue