Function with type 'T -> Async<'T> like C#'s Task.FromResult

  • A+
Category:Languages

I'm playing around asynchronous programming and was wondering if there's a function that exists that can take a value of type 'T and transform it to an Async<'T>, similar to C#'s Task.FromResult that can take a value of type TResult and transform it to a Task<TResult> that can then be awaited.

If such a function does not exist in F#, is it possible to create it? I can kind of emulate this by using Async.AwaitTask and Task.FromResult, but can I do this by only using Async?

Essentially, I'd like to be able to do something like this:

let asyncValue = toAsync 3 // toAsync: 'T -> Async<'T>  let foo = async{         let! value = asyncValue } 


You can use return within your async expression:

let toAsync x = async { return x } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: