Open Seng-Jik opened 1 year ago
I guess this can be useful. Although assuming that the returned Async represents the still-running other computation, the semantics are more fitting for Task than Async.
Personally, I'm not entirely sure it has to be in the standard library, as opposed to just having it in nuget package.
This pretty much already exists as Choice returning things like Choice<A,B>
from it is a pretty thin wrapper. Doesn't feel like something that needs to be put into core.
Async.Choice is a thing, which ever finishes first returning first
If you are implementing this api probably return a Task to represent on-going task that can be waited for its result, instead of returning async which is more like a specification in F#, of course you can wrap the result being waited in specification that specifies you can wait on it until result is available...
Functionally speaking, you produce a bunch of Task (not .NET task, task in general), checking whether the task has finished / value / result and iterate on them again clean up the rest of the tasks
Yeah, my gut feel is this belongs in an async extensions library.
FWIW there are some proposals in this space that are parked as TaskEx
issues in https://github.com/fsprojects/FSharp.Control.TaskSeq/issues
Anything that joins that list should generally meet the bar of being applicable across Async/Task/ValueTask
Has anyone got a sample implementation based on existing Async?
I propose we add the these functions to FSharp.Core:
Async.RaceAwait(a: Async<'a>, b: Async<'b>) : Async<Choice<'a * Async<'b>, Async<'a> * 'b>>
Await two async computation in same time, ifa
is first finished, returns Choice1of2, otherwise returns Choice2of2.Async.RaceAwaitSimple(a: Async<'a>, b: Async<'a>) : Async<'a * Async<'a>>
Simple version ofAsync.RaceAwait
.Async.RaceAwaitSeq(a: seq<Async<'a>>) : Async<'a, seq<Async<'a>>>
Await allAsync<'a>
ina
, and return fastest result in'a
, another results inseq<Async<'a>>
.Pros and Cons
The advantages of making this adjustment to F# is having a useful function directly accessible in an idiomatic way without the need to resort to a package or using hand-written code.
The disadvantages of making this adjustment to F# is: Having more and more functions in the core library could make it harder for beginners to see the essentials.
Extra information
Estimated cost XS
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.