【问题标题】:Rxjs throw timeout error from pipe to observable error sectionRxjs 将超时错误从管道抛出到可观察的错误部分
【发布时间】:2020-01-03 02:50:43
【问题描述】:

我试图将可观察管道中的超时错误作为错误抛出给观察者,但我仍然在 val 回调(订阅中)中收到超时错误,而我想在错误回调中得到它:

对于谁进行实验,您可以在此链接中轻松测试此代码并在 stackblitz 中对其进行编辑:

https://stackblitz.com/edit/typescript-eegqyz?file=index.ts&devtoolsheight=100

of(4000, 3000, 2000)
    .pipe(
        concatMap(duration =>
            makeRequest(duration).pipe(
                timeout(2500),
                catchError(error => {
                    //throwError('Valid token not returned');
                    return of(`Request timed out after: ${duration}`)
                })
            )
        )
    )
    .subscribe(
        val => console.log(val),
        error => console.log("error", error)
    );

【问题讨论】:

    标签: angular rxjs observable rxjs6 redux-observable


    【解决方案1】:

    这是我设法解决的方法:

    of(4000, 3000, 2000)
      .pipe(
        concatMap(duration =>
          makeRequest(duration).pipe(
            timeout(2500),
            catchError(err => throwError(`Request timed out after: ${duration}`))
          )
        ),
      )
    

    所以,基本上你有办法处理来自 timeout 的错误(这就是为什么你在第一个 cb 中从 subscribe() 得到错误),现在如果抛出另一个错误(使用 throwError),没有其他地方可以处理传入的错误,因此您将在其回调中收到错误(来自subscribe())。

    Here is a StackBlitz example.

    【讨论】:

      猜你喜欢
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2017-03-23
      • 2021-06-17
      • 2014-02-24
      • 1970-01-01
      相关资源
      最近更新 更多