Observables vs promises

Observables compared to other techniques

You can often use observables instead of promises to deliver values asynchronously. Similarly, observables can take the place of event handlers. Finally, because observables deliver multiple values, you can use them where you might otherwise build and operate on arrays.

Observables behave somewhat differently from the alternative techniques in each of these situations, but offer some significant advantages. Here are detailed comparisons of the differences.

 

Observables compared to promises

Observables are often compared to promises. Here are some key differences:

  • Observables are declarative; computation does not start until subscription. Promises execute immediately on creation.
    This makes observables useful for defining recipes that can be run whenever you need the result.
  • Observables provide many values. Promises provide one.
    This makes observables useful for getting multiple values over time.
  • Observables differentiate between chaining and subscription. Promises only have .then() clauses.
    This makes observables useful for creating complex transformation recipes to be used by other part of the system, without causing the work to be executed.
  • Observables subscribe() is responsible for handling errors. Promises push errors to the child promises.
    This makes observables useful for centralized and predictable error handling.

 

 

Creation and subscription

  • Observables are not executed until a consumer subscribes. The subscribe() executes the defined behavior once, and it can be called again. Each subscription has its own computation. Resubscription causes recomputation of values.
    // declare a publishing operation
    const observable = new Observable<number>(observer => {
      // Subscriber fn...
    });
    
    // initiate execution
    observable.subscribe(value => {
      // observer handles notifications
    });

     

  • Promises execute immediately, and just once. The computation of the result is initiated when the promise is created. There is no way to restart work. All then clauses (subscriptions) share the same computation.
    // initiate execution
    let promise = new Promise<number>((resolve, reject) => {
      // Executer fn...
    });
    promise.then(value => {
      // handle result here
    });

     

Chaining

  • Observables differentiate between transformation function such as a map and subscription. Only subscription activates the subscriber function to start computing the values.
    observable.pipe(map(v => 2 * v));
  • Promises do not differentiate between the last .then clauses (equivalent to subscription) and intermediate .then clauses (equivalent to map).
    promise.then(v => 2 * v);

Cancellation

  • Observable subscriptions are cancellable. Unsubscribing removes the listener from receiving further values, and notifies the subscriber function to cancel work.
    const subscription = observable.subscribe(() => {
      // observer handles notifications
    });
    
    subscription.unsubscribe();

Error handling

  • Observable execution errors are delivered to the subscriber’s error handler, and the subscriber automatically unsubscribes from the observable.
    observable.subscribe(() => {
      throw new Error('my error');
    });
  • Promises push errors to the child promises
    promise.then(() => {
      throw new Error('my error');
    });

Leave a Reply