Hacker News new | ask | show | jobs
by mjackson 4367 days ago
> FRP streams go beyond promises by allowing many returned values. This is pretty nice, and shows how FRP is at least as powerful as Promises.

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.

2 comments

> In synchronous code, you can only return once from a function.

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.

Or, over in C land, setjmp.
A stream can be thought of like a function in a lazy language returning an array of results which aren't necessarily calculated (or available) all at once.

>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.