rehansaeed.github.io
rehansaeed.github.io copied to clipboard
[Comment] Reactive Extensions (Rx) - Part 6 - Task ToObservable
https://rehansaeed.com/reactive-extensions-part6-task-toobservable/
Dennis Daume commented on 2014-04-25 20:19:20
I think instead of obs1.Merge(obs2).FirstAsync you can write obs1.Amb(obs2) to get the first responding sequence :)
Daniel commented on 2015-12-29 09:37:32
Thank you for showing clear, practical code! I think the TPL vs Rx solutions in the timeout example aren't doing the same thing though:
- TPL: the
Task.Delay()runs in parallel, making the timeout count from the start. - Rx:
.Timeout()counts from last item yielded (http://reactivex.io/documentation/operators/timeout.html)
Muhammad Rehan Saeed commented on 2016-01-07 16:22:26
Thank you for showing clear, practical code! I think the TPL vs Rx solutions in the timeout example aren't doing the same thing though:
- TPL: the
Task.Delay()runs in parallel, making the timeout count from the start.- Rx:
.Timeout()counts from last item yielded (http://reactivex.io/documentation/operators/timeout.html)
Yes but there is only one item yielded from each observable. Perhaps I have misunderstood you.
Daniel commented on 2016-01-10 21:37:30
Yes but there is only one item yielded from each observable. Perhaps I have misunderstood you.
True, my bad, thanks for the response.
Guan George commented on 2017-11-17 21:05:34
Very good and helpful. Thanks for your blog. ^_^
One suggestion, Task.Run(async...) can be removed like this:
public async static Task GetHelloString()
{
await Task.Delay(2000);
return "Hello";
}
One correction: should we use await await instead of await cause WhenAny returns Task<Task>?
if only one await, complier will not succeed (At least in my VS 2015 ^_^).
public async static Task<string> WaitForFirstResultAndReturnResultWithTimeOut2()
{
Task<string> task1 = GetHelloString();
Task<string> task2 = GetWorldString();
return await await Task.WhenAny(task1, task2)
.ToObservable()
.Timeout(TimeSpan.FromMilliseconds(500))
.FirstAsync();
}
I started learn C# 1 week ago, so my option may be wrong. Sorry if.
Muhammad Rehan Saeed commented on 2017-11-20 08:35:16
Very good and helpful. Thanks for your blog. ^_^
One suggestion,
Task.Run(async...)can be removed like this:public async static Task GetHelloString() { await Task.Delay(2000); return "Hello"; }One correction: should we use
await awaitinstead ofawaitcauseWhenAnyreturnsTask<Task>? if only oneawait, complier will not succeed (At least in my VS 2015 ^_^).public async static Task<string> WaitForFirstResultAndReturnResultWithTimeOut2() { Task<string> task1 = GetHelloString(); Task<string> task2 = GetWorldString(); return await await Task.WhenAny(task1, task2) .ToObservable() .Timeout(TimeSpan.FromMilliseconds(500)) .FirstAsync(); }I started learn C# 1 week ago, so my option may be wrong. Sorry if.
Task.WhenAny should not be returning a task of task unless you are doing something very strange.
Frank commented on 2018-01-23 15:47:51
I am not sure yet, as I have not tested, but I think I see a need for this in Linq code of the kind from x in y select f(x), where f is an async function and the result is an IObservable<f(x)>.