WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
563 stars 13 forks source link

Include `finally` in the methods. #18

Closed benlesh closed 1 year ago

benlesh commented 1 year ago

finally is important, as it behaves like promise's finally, in that it will fire a passed handler if the observable errors or completes... however it will also fire if the user unsubscribes from the stream. It's basically just a way to ensure some related teardown happens. Finally is implemented like so:

finally(finalizer: () => void): {
  return new Observable((subscriber) => {
    const subscription = source.subscribe(subscriber)
    return () => {
      subscription.unsubscribe();
      finalizer();
    }
  })
}
domfarolino commented 1 year ago

Added. Thanks!