What is the correct way to use concat along with pipe in Rxjs 6?
Asked Answered
B

1

9

I have a server call which may return HTTP 202. Influenced by this SO thread, I have the following:

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

Getting a deprecated warning on the use of concat. I understand the new concat is in rxjs and not rxjs/operator.

But, what is the right way to use the new static concat operator here?

Found the following from this site

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

// becomes

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

I'm not able to wrap my head around which Observables are being concatenated in my example code?

UPDATE 1

Changed the code to:

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

But this results in:

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 
Bauxite answered 27/7, 2018 at 22:3 Comment(0)
L
13

Pipeable operators are just functions that take an observable and return an observable. So you can use the concat factory function like this:

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

Also, the concat operator is going to be replaced with/renamed to concatWith. See this issue.

Lowland answered 28/7, 2018 at 3:20 Comment(1)
Changed throw to throwError for v6.xBauxite

© 2022 - 2024 — McMap. All rights reserved.