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 Observable
s 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
The RxJS concat() operator is a join operator that creates an output Observable, which sequentially emits all values from the given Observable and then proceed to the next one.
Pipes let you combine multiple functions into a single function. The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.
The pipe method of the Angular Observable is used to chain multiple operators together. We can use the pipe as a standalone method, which helps us to reuse it at multiple places or as an instance method.
There are over 100 operators in RxJS that can be classified into various categories, including creation, transformation, filtering, joining, multicasting, error handling, and utility.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With