fp-ts-rxjs
fp-ts-rxjs copied to clipboard
Suggestion: provide alternatives to operators which throw exceptions / emit errors
🚀 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.
Sounds good. We could name them analog to the Array
methods head
and tail
.
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
.
- never throws
- returns
ObservableEither<FetchError | ResponseJsonParseError, Either.Json>
https://stackblitz.com/edit/fp-ts-rxjs-fromfetch?file=index.ts
Update: https://github.com/unsplash/request-frp