In my everlasting quest to suck less I'm trying to understand Rx.net's FirstAsync()
syntax. Most documentation is for the deprecated First()
If I understand correctly it allows me to start a stream as soon as the first element from a stream arrives.
Say I have a stream myStream
and I want to start a stream that takes the first element and starts a stream based on that one element. In my case it would be a stream of one.
I expect it to do this:
---1-2->
---A--->
How would I go about this?
myStream.FirstAsync().Return(() => return "A"); // doesn't compile
I don't know why the other two answers are saying .FirstAsync()
returns a Task
(or that you should call .Result
). It does not return a Task
, it returns an IObservable<TSource>
. Observables are awaitable, but they are not tasks.
To achieve your desired functionality, do the following: myStream.FirstAsync().Select(_ => "A")
.
You can also do myStream.Take(1).Select(_ => "A")
. The difference between this and the FirstAsync
version, is that the FirstAsync
version will throw an exception if myStream
completes without any elements. Take(1)
will complete with no error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With