question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Analog to `Rx.Observable.toPromise()` ?

See original GitHub issue

One 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:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
briancavaliercommented, Aug 10, 2017

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.

const retrieveLast = stream => stream.reduce((_, v) => v)

const retrieveFirst = stream => retrieveLast(stream.take(1))

For the larger community, I think this could be a nice mostjs-community package. Is that something you’d be interested in contributing?

Cheers!

0reactions
briancavaliercommented, Aug 12, 2017

@cloderic Awesome, and we released most 1.6.0 with the thru return type update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is analogous to Promise.resolve for an Observable?
In this example, the CONTACTS object is static JSON. A promise isn't necessary here, but was used to show usage of a service...
Read more >
toPromise - Learn RxJS
toPromise is not a pipable operator, as it does not return an observable. ​. ​​. Ultimate RxJS. ​​. Examples. Example 1: Basic Promise....
Read more >
toPromise behavior when observable ends without having ...
In Rx, observer.complete() implies a successful resolution of the observable sequence, that is totally independent of whether or not the ...
Read more >
Conversion to Promises - RxJS
The lastValueFrom is almost exactly the same as toPromise() meaning that it will resolve with the last value that has arrived when the...
Read more >
Overview - ReactiveX
Introduction. RxJS is a library for composing asynchronous and event-based programs by using observable sequences. It provides one core type, the Observable ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found