【问题标题】:What is the correct way to use concat along with pipe in Rxjs 6?在 Rxjs 6 中使用 concat 和管道的正确方法是什么?
【发布时间】:2019-01-05 01:24:17
【问题描述】:

我有一个可能返回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)
  );

在使用concat 时收到deprecated 警告。我知道新的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$);

我无法理解在我的示例代码中连接了哪些Observables?

更新 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 

【问题讨论】:

    标签: angular rxjs angular-httpclient rxjs6


    【解决方案1】:

    Pipeable 操作符只是接受一个 observable 并返回一个 observable 的函数。所以你可以像这样使用concat 工厂函数:

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

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

    【讨论】:

    • 在 v6.x 中将 throw 更改为 throwError
    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2018-09-24
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多