tc39 / proposal-observable

Observables for ECMAScript
https://tc39.github.io/proposal-observable/
3.06k stars 90 forks source link

Should subscribe's next callback be optional like error/complete callbacks #170

Open lrowe opened 7 years ago

lrowe commented 7 years ago

Observable.prototype.subscribe may be called either with a single Observer object, or up to three callbacks functions for next/error/complete. The error and complete callbacks are optional, however since the spec uses IsCallable(observer) is true to decide whether to branch and use callbacks, the next callback is not currently optional.

If the branch check were instead equivalent to !(typeof observer === 'object' && observer !== null) then the next callback would be optional and we could write observable.subscribe(undefined, undefined, () => console.log("complete")).

This has the additional benefit of allowing observable.subcribe() to be called without parameters when you want to start an observable but do not need to observe the output.

Edits:

benjamingr commented 7 years ago

That sounds like a very reasonable change.

zenparsing commented 7 years ago

What happens if you do something like:

observable.subscribe('bleh');

Originally this would have been a TypeError, but now I think it acts like observable.subscribe({}). Is that what we want?

lrowe commented 7 years ago

I think it would be equivalent to observable.subscribe({ next: 'bleh' }) which would result in an error when subscriptionObserver.next(value) was called. Throwing the TypeError from subscribe would certainly provide a better developer experience.

The same issue is present today with observable.subscriber(console.log, 'bleh').