gcanti / fp-ts-rxjs

fp-ts bindings for RxJS
https://gcanti.github.io/fp-ts-rxjs/
MIT License
188 stars 29 forks source link

Suggestion: provide alternatives to operators which throw exceptions / emit errors #55

Open OliverJAsh opened 3 years ago

OliverJAsh commented 3 years ago

🚀 Feature request

Current Behavior

In RxJS, some operators are known to throw exceptions intentionally, in non-exceptional circumstances.

For example, the first operator will throw an EmptyError if the observable completes after having emitted no items (an empty observable). https://rxjs-dev.firebaseapp.com/api/operators/first

Other operators which throw: last, single, and the forthcoming lastValueFrom/firstValueFrom (RxJS 7).

Desired Behavior

Operators which don't throw.

Suggested Solution

For example, this library could provide its own first operator which would return Observable<Option<T>>. If the observable is empty, the observable would emit none instead of throwing an exception / emitting an error.

mlegenhausen commented 3 years ago

Sounds good. We could name them analog to the Array methods head and tail.

OliverJAsh commented 3 years ago

Here's another one: fromFetch. If the user loses connectivity by the time you subscribe to the observable, fetch will reject and thereby the observable will emit an error.

Here's a wrapper we've been using at Unsplash around RxJS' fromFetch.

https://stackblitz.com/edit/fp-ts-rxjs-fromfetch?file=index.ts

Update: https://github.com/unsplash/request-frp