0

How do you await nongeneric Tasks in F# async workflows?

open System.IO

let openConnection = async { ... }

let answer = async {
    use! stream = openConnection
    let reader = new StreamReader (stream)
    let! request = reader.ReadLineAsync () |> Async.AwaitTask // works fine: Task<String>
    ...
    let writer = new StreamWriter (stream)
    do! writer.WriteLineAsync "OK" |> Async.AwaitTask // error: Task
}

Visual Studio complains that the type Task is not compatible with type Task<'a>. Is there a way to transform the Task to Task<Unit>?

Using Visual Studio 2012 & F# 3.0...

Nikon the Third
  • 2,771
  • 24
  • 35
  • Yeah, this is what I was looking for. The search for "nongeneric" yielded no results for me. Thanks. – Nikon the Third Sep 17 '13 at 14:14
  • Hi, maybe you can use (writer.WriteLineAsync "OK").Wait() or do! async {writer.WriteLine "OK"}, for asynchronous use (writer.WriteLineAsync "OK").Start() or async {writer.WriteLine "OK"} |> Async.Start – kwingho Sep 17 '13 at 17:18
  • The linked question provided exactly the answers I was looking for. The code you suggested wouldn't work for me because the workflow should only continue after the `WriteLineAsync` call has finished, and not simply start a new Task and go on. – Nikon the Third Sep 18 '13 at 13:49

0 Answers0