BACK
Lessons33. An error is a terminal event
Lessons · 33

33. An error is a terminal event

An experiment: an error is the end

This is a short but important lesson before we study error handling. You need to see it with your own eyes: after error, the stream is dead. No later next or complete calls will reach the subscriber.

Why this matters

It's not "just another value that happens to be an error." It's a final signal. If an error occurs somewhere in the pipe chain, it "kills" the whole stream down to the subscriber — unless we catch it (which we'll cover in the next lessons).

The formal contract

next* (error | complete)?
// Any number of next in a row.
// Then either an error or a completion — and nothing more.

Your task

  1. In the recipe function, call in order: subscriber.next('A'), then subscriber.error('Boom').
  2. Then deliberately try to call subscriber.next('B') and subscriber.complete().
  3. Run the code and confirm: the logs contain only Next: A and Error: Boom. B and Complete never appear — RxJS "ignored" them.
Solution spoiler · click to reveal
const { Observable } = Rx;

const source$ = new Observable(subscriber => {
  subscriber.next('A');
  subscriber.error('Boom');
  subscriber.next('B');
  subscriber.complete();
});

source$.subscribe({
  next: value => console.log('Next: ' + value),
  error: error => console.log('Error: ' + error),
  complete: () => console.log('Complete'),
});
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...