ReactiveX / IxJS

The Interactive Extensions for JavaScript
https://reactivex.io/IxJS/
MIT License
1.33k stars 74 forks source link

source.first() has ambiguous behavior. #275

Closed cwharris closed 4 years ago

cwharris commented 5 years ago

IxJS version: 2.5.3

Code to reproduce:

try {
  const first = source.first();
  // first yields any number of values, the first of which could be undefined.
} catch (ex) {
  // invalid operation, there is no such thing as first.
}

Expected behavior: first throws an exception when no value is yielded from source.

Actual behavior: first ambiguously yields undefined if no value is yielded from source.

Additional information: first currently exhibits the ideal behavior of hypothetical firstOrDefault() / firstOrDefault(undefined) / first(undefined).

firstOrDefault can be implemented in terms of a first which throws on no yield, but first should not be implemented in terms of firstOrDefault.

If backwards compatibility is strictly necessary, suggest adding a flag to first, or creating new function firstOrError.