Replay Subject用angular async收到error的行為

找到一篇文章 擷取其中一段話

The basic concept of RxJS is that any error or complete-call will basically “kill” a stream. This concept forces you not “just to throw around errors here and there as you please” but to handle errors and the flow of data within your application properly.

簡單的說當收到 error()complete() 時,資料流會被結束。可是有個有趣的地方,next() 出來的資料會收不到,但是error還是收的到。

範例

  • 情境:

    • 建立一個replaySubject,設定replay最後兩結果,並在template中用async訂閱
    • OnInit時,以間隔0.5秒,先發出三個next(),再發出二個error(),再發出一個next(),最後發出error()。
    • 在4秒時,訂閱replaySubject
  • 結果:

    • async 訂閱的資料會收到 3,且error()發出之後不再收到新資料
    • 在第4秒訂閱並顯示在console的資料,只出現2、3, error() 出現後的 next 不會被replay
    • 還是會收到最後產生的 error 資料