finally() is not called when returning empty() in catch()

浪尽此生 提交于 2019-12-12 04:25:14

问题


I wrote a generic request method that is supposed to show a loading indicator while the request is running. If an error occurs (like 404), I display the message in .catch and then return Observable.empty(), so that the following code doesn't crash (since no data is returned).

The big problem is that then .finally won't be called either. Is that a bug? Is there a workaround? Here's my code:

res = Observable
  .of(true)
  .do(() => this.store.dispatch(new ShowLoadingIndicatorAction()))
  .switchMap(() => this.http.get(url, { headers: this.headers }))
  .publishReplay(1)
  .refCount()
  .catch(error => {
    this.messages.handleRequestError(error);
    return Observable.empty();
  })
  .finally(() => this.store.dispatch(new HideLoadingIndicatorAction()));

// then later:
res.subscribe(doStuffWithData);

回答1:


What RxJS-version are you using? It does works fine in this example:

const res$ = Rx.Observable
  .of(true)
  .switchMap(() => Rx.Observable.throw("Rest Error"))
  .publishReplay(1)
  .refCount()
  .catch(error => Rx.Observable.empty())
  .finally(() => console.log("Calling Finally!"));

res$.subscribe(console.info);
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>


来源:https://stackoverflow.com/questions/41999077/finally-is-not-called-when-returning-empty-in-catch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!