admin管理员组文章数量:1330564
I want to check one api call inside an observable which I will subscribe in a ponent. As written below, I want to run my observable in this manner but it is not working. What changes shall I do to this code to make it work. Whenever I try to subscribe through it especially through the scenario when someObservableWrittenInTheSameService returns with an error 404, I want to return url2.
getfunction(submissionId: string ){
if (some condition) {
this.someObservableWrittenInTheSameService(parameter).subscribe(
(httpValue: any) => {
let url = '';
if (httpValue.code === 200) {
return this.http.get(url1);
}
}, err => {
if (err.code === 404) {
return this.http.get(url2);
}
}
)
}
let url3
return this.http.get(url3);
}
This function is then is called in a ponent where it is subscribed. But whenever someObservableWrittenInTheSameService return 404, the subscription always fails and go to error block in the ponent.
I want to check one api call inside an observable which I will subscribe in a ponent. As written below, I want to run my observable in this manner but it is not working. What changes shall I do to this code to make it work. Whenever I try to subscribe through it especially through the scenario when someObservableWrittenInTheSameService returns with an error 404, I want to return url2.
getfunction(submissionId: string ){
if (some condition) {
this.someObservableWrittenInTheSameService(parameter).subscribe(
(httpValue: any) => {
let url = '';
if (httpValue.code === 200) {
return this.http.get(url1);
}
}, err => {
if (err.code === 404) {
return this.http.get(url2);
}
}
)
}
let url3
return this.http.get(url3);
}
This function is then is called in a ponent where it is subscribed. But whenever someObservableWrittenInTheSameService return 404, the subscription always fails and go to error block in the ponent.
Share Improve this question asked Mar 7, 2022 at 15:52 Aman AhmadAman Ahmad 131 gold badge1 silver badge4 bronze badges 4-
You should watch
catchError
operator to check if it match your expectations rxjs.dev/api/operators/catchError – Maxime Chevallier Commented Mar 7, 2022 at 15:55 -
Your question is not clear
This function is then is called in a ponent where it is subscribed. But whenever someObservableWrittenInTheSameService return 404, the subscription always fails and go to error block in the ponent.
. This is working as expected – brk Commented Mar 7, 2022 at 16:01 - @MaximeChevallier Yeah solution would involve this. – Aman Ahmad Commented Mar 8, 2022 at 17:20
- @brk It is not working as intended. I followed the approach given by ruth to make it work. – Aman Ahmad Commented Mar 8, 2022 at 17:22
1 Answer
Reset to default 4- You could use RxJS
iif
function to return an observable conditionally. - Use RxJS higher order mappping operator
switchMap
to map from one observable to another. More info here. - Use
catchError
operator to perform error handling. From it's body you could either return the HTTP request or forward the error (usingthrowError
) or even plete the observable (usingEMPTY
constant) based on your requirement.
Try the following
import { Observable, EMPTY, iif, throwError } from 'rxjs';
import { switchMap, catchError } from 'rxjs/operators';
getfunction(submissionId: string): Observable<any> { // <-- observable must be returned here
const obs1$ = this.someObservableWrittenInTheSameService(parameter).pipe(
switchMap((httpValue: any) =>
iif(
() => httpValue.code === 200,
this.http.get(url1),
EMPTY // <-- plete the observable if code is other than 200
)
),
catchError((error: any) => // <-- `catchError` operator *must* return an observable
iif(
() => error.code === 404,
this.http.get(url2),
throwError(error) // <-- you could also return `EMPTY` to plete the observable
)
)
const obs2$ = this.http.get(url3);
return iif(
() => someCondition,
obs1$,
obs2$
);
}
In this case you'd subscribe to the getFunction()
function where it's used.
For eg.
this.getFunction('some value').subscribe({
next: (value: any) => { },
error: (error: any) => { },
plete: () => { }
});
本文标签:
版权声明:本文标题:javascript - How to handle error and return observable while subscribe inside an observable function in Rxjs Angular - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742217582a2434850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论