且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RxJS Angular2在Observable.forkjoin中处理404

更新时间:2023-10-19 21:03:28

我认为您可以使用 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
  });
}