RxJS Angular2在Observable.forkjoin中处理404 [英] RxJS Angular2 handling 404 in Observable.forkjoin
本文介绍了RxJS Angular2在Observable.forkjoin中处理404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在链接一堆http请求,但是我在订阅之前无法处理404错误。
I'm currently chaining a bunch of http requests, however I am having trouble handling 404 errors before subscribing.
我的代码:
:
...
service.getData().subscribe(
data => this.items = data,
err => console.log(err),
() => console.log("Get data complete")
)
...
在服务中:
...
getDataUsingUrl(url) {
return http.get(url).map(res => res.json());
}
getData() {
return getDataUsingUrl(urlWithData).flatMap(res => {
return Observable.forkJoin(
// make http request for each element in res
res.map(
e => getDataUsingUrl(anotherUrlWithData)
)
)
}).map(res => {
// 404s from previous forkJoin
// How can I handle the 404 errors without subscribing?
// I am looking to make more http requests from other sources in
// case of a 404, but I wouldn't need to make the extra requests
// for the elements of res with succcessful responses
values = doStuff(res);
return values;
})
}
推荐答案
我认为您可以使用 catch
运算符。发生错误时将调用您在调用时提供的回调:
I think you could use the catch
operator. The callback you provide when calling it will be called when an error will occur:
getData() {
return getDataUsingUrl(urlWithData).flatMap(res => {
return Observable.forkJoin(
// make http request for each element in res
res.map(
e => getDataUsingUrl(anotherUrlWithData)
)
)
}).map(res => {
// 404s from previous forkJoin
// How can I handle the 404 errors without subscribing?
// I am looking to make more http requests from other sources in
// case of a 404, but I wouldn't need to make the extra requests
// for the elements of res with succcessful responses
values = doStuff(res);
return values;
})
.catch((res) => { // <-----------
// Handle the error
});
}
这篇关于RxJS Angular2在Observable.forkjoin中处理404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文