Exploring error handling patterns and strategies
The first classic pattern we will learn for handling errors is based on the subscribe()
method. The subscribe()
method takes as input the object Observer, which has three callbacks:
- A success callback: This is called every time the stream emits a value and receives as input the value emitted
- An error callback: This is called when an error occurs and receives as input the error itself
- A completion callback: This is called when the stream completes
This is a basic example of a subscribe
implementation:
stream$.subscribe({ next: (value) => console.log('Value Emitted', value), error: (error) => console.log('Error Occurred', error), complete: () => console.log('Stream Completed'), });
In the code sample, stream$
represents our Observable, and we passed an object that has three callbacks to the subscribe...