by mjackson · 12y ago · 0 comments
The fact that you can only return a single value from a promise is a feature, not a limitation. In synchronous code, you can only return once from a function. In asynchronous promise code, you can only fulfill the promise once. There's a parallel.
If you want to "allow many returned values" you can just use a callback. You hardly need a "stream" for that.
Erm, have you heard a term "continuation" or "call/cc" ("call-with-curent-continuation")? Or "delimited continuations"? Or maybe even "Cont monad"? You should read on them, if you haven't - they are one of the most fundamental and fun concepts I encountered.
But even without those, what you wrote here is false in the presence of generators (which are coming to JS any day now!).
I'd say that returning more than once from function is actually a pretty basic technique which is widely used to implement quite a wide array of different things.
>If you want to "allow many returned values" you can just use a callback. You hardly need a "stream" for that.
Callbacks are hard to compose, may not have standard error conventions, and you can't have multiple listeners subscribe to the same thing after the fact. (Those are the same things that promises solve for async functions with a single result.) Additionally, you can stop listening to a stream and the source can know to close itself once it has no more listeners.