Suppose I have an async data source:
let getData() = async { return [ 3.14; 2.72 ] }
I could call it using let!
and a temporary label:
let showData1() = async {
let! data = getData()
data
|> Seq.iter (printfn "%A")
}
Or, I could call it (inefficiently!) using Async.RunSynchronously
and piping, and without a temporary label:
let showData2() = async {
getData()
|> Async.RunSynchronously
|> Seq.iter (printfn "%A")
}
I like the syntax of showData2
but know that calling Async.RunSynchronously
ties up the calling thread.
Is there a syntax, operator, or function defined somewhere that allows me to pipe an Async<'T>
into a function that accepts 'T
?
It sounds like you want map
for Async
:
let mapAsync f a = async {
let! v = a
return (f v)
}
then you can do:
let m: Async<unit> = getData() |> mapAsync (Seq.iter (printfn "%A"))