Comments (8)
@benjamingr The problem that we mentioned in the other thread is that we don't want to mix error handling mechanisms. For instance:
let s = obs.subscribe({
next(x) {},
error(x) {},
});
s.catch(x => {});
This would confuse error handling.
I would like to introduce a counter-proposal:
- We eliminate
Symbol.observer
(synchronous subscription) and makesubscribe
synchronous, like Rx. The "subscribe" method has no zalgo-protection. forEach
performs subscription asynchronously and has zalgo-protection and returns a normal promise. The error or return values are channelled through the promise.- The argument to
forEach
accepts a value and a subscription object, which can be used to short-circuit.
// No zalgo-protection with subscribe
let subscription = observable.subscribe({ next, error, complete });
// forEach subscribes in a job, and flows errors and completion through the promise
observable.forEach((value, subscription) => {
whatever;
}).then(
returnValue => {},
errorValue => {}
);
from proposal-observable.
@benjamingr The problem that we mentioned in the other thread is that we don't want to mix error handling mechanisms. For instance:
Why? Can't we just let catch
for the observer run first and if no handler is attached or it threw let the promise handle it. In this proposal, the promise has no meaningful return value anyway so "value recovery" isn't a problem here.
from proposal-observable.
That is: the catch
on the promise only signifies that the observer terminated abruptly and that it didn't handle it in a catch
, this has the benefit of aligning with async/await
and throwing in the surrounding scope as well as benefiting from existing unhandled promise rejection tracking mechanisms
from proposal-observable.
I like @zenparsing's proposal better, and I agree, I think it's best to keep the subscription type simple. If we go to the extreme the other way, you could just about make a Subject out of the Subscription.
from proposal-observable.
I see a ton of benefit to being able to await subscribe
and not so many downsides. The channeling of error through the promise in forEach
happens either way (since no error
) - remember, making it a then
able is free in this case - no promise needs to be allocated per subscription unless you use it.
In both proposals you can make a subject out of the subscriber that's not really an issue with either.
I'm also +1 on forEach
having zalgo protection but I'm still interested in seeing how subscribe
can be made safer (that's another issue though). I'm also +1 on an additional subscription
argument in every next
callback, be it forEach
or subscribe
I really like that idea.
from proposal-observable.
I think it's semantically weird:
myObservable.subscribe(blah).then(doSomething);
Reads like: "Once you're subscribed, then do something". subscribe
is usually the end of the chain.
from proposal-observable.
Right, to be fair in 99.9% of cases most people would be using it as:
await myObservable.subscribe({ next(v){
doSomethingWith(v);
/*...*/});
It's just useful to await
subscribe
in my opinion and I expect I'll use it in my code if it's available. It bothers me that if I forEach
I can't access the callbacks using the observer interface - making any code that utilizes it (like specific instances of observer I create) impossible to use in this scenario. This is more painful if I implement a Subject
in my code and I want to run the subject and await the subscription.
Given a then
can be a getter and is free and it's useful, I'm not sure why we wouldn't want to have it for async/await
support. It also means we can pass subscriptions to code using promises and it'll wait for the sequence to finish which is also useful.
from proposal-observable.
@benjamingr Mixing async primatives at a basic level (i.e. subscribe
) is going to be problematic. It can be done, but the clean way to do things is to keep them separated.
I am interested in a forEach
-style alternate subscription function which only accepts a single callback and returns a promise for the return value or error. However, such a subscription function would essentially be an async task and until we have a story for cancellation of async tasks...
As I note at the end of the README, forEach
is deferred for now.
To be honest, I'm actually more interested in allowing observables to be async-iterable:
async function af(observable) {
for async (let x of observable) {
// process observed values, throw if error, etc.
}
}
from proposal-observable.
Related Issues (20)
- Invalid test based on Interface. HOT 1
- `obs.subscribe(next, error, complete)` should bind their callbacks to `undefined` when present HOT 5
- Why does `Observable.prototype.subscribe` report thrown errors from `observer.start(sub)` asynchronously instead of just propagating them?
- Minor spec bug WRT cleanup in `subscribe`
- `Observable.from` iteration functions incorrectly assume their observer parameter is native HOT 1
- [ALTERNATIVE] Proposal for an alternative
- Cleanup function should be passed to the SubscriptionObserver
- Simplification of Observable API HOT 69
- End a subscription if a completion token is returned HOT 1
- Even simpler API HOT 4
- Reduced API with async/await support HOT 23
- Observable should be async HOT 5
- Syntax Support HOT 4
- Alternative: Pub/Sub
- Moving to an API with AbortSignal HOT 9
- Retain core API and leave operators to user-land libraries HOT 15
- Permit unsubscribe to return a promise HOT 3
- Is there any update? HOT 34
- Support [Symbol.dispose]() for unsubscribe() HOT 1
- Unsubscribe
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-observable.