Analog to `Rx.Observable.toPromise()` ?
See original GitHub issueOne of the nicest things about most is how well it plays with Promises. However unless I miss something there’s only one way to extract values from the stream to a promise and that’s stream.reduce
. Obviously you can achieve everything you need with reduce
but it can be hard to read and/or a pain to implement.
A common use case I have is doing some manipulation on a stream so it ends up with only one value using a combination of stream.filter
, stream.take
, most-buffer
etc. and finishing that with an ugly .reduce((_, v) => v)
which is not the most readable line.
Rx observables have a toPromise()
function that actually resolves to the last event in the stream. I’m not a big fan as it is to really explicit as to which value is taken.
So here the proposal.
stream.retrieveFirst()
which is sugar for
stream.take(1).reduce((_, v) => v)
and
stream.retrieveLast()
which is sugar for
stream.reduce((_, v) => v)
What do you think?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (8 by maintainers)
Hey @cloderic, thanks for opening the discussion.
For your own usage, you can just wrap these up in functions, and then just call them, or use them with thru.
For the larger community, I think this could be a nice mostjs-community package. Is that something you’d be interested in contributing?
Cheers!
@cloderic Awesome, and we released most 1.6.0 with the
thru
return type update.