I'v an async function that returns a string asynchronously and I'm calling that function within test method and is throwing computation expressions, what would be the possible fix?
Code
let requestDataAsync (param: string) : Async<string> =
async {
Console.WriteLine param
return "my result"
}
Test Code
[<TestMethod>]
member this.TestRequestDataAsync() =
let! result = requestDataAsync("some parameter")
Console.WriteLine(result)
Assert.IsTrue(true)
Error for this line let! result = requestDataAsync("some parameter")
This construct can only be used within computation expressions
Question, How to wait and display the result of the async function?
Calls to let!
must appear at the top level of the computation expression. You can fix it by creating a nested async { } block:
[<TestMethod>]
member this.TestRequestDataAsync() =
async
{
let! result = requestDataAsync("some parameter")
Console.WriteLine(result)
Assert.IsTrue(true)
}
The answer would be to use RunSynchronously. that way you can wait on the function call.
let result = "some parameter" |> requestMetadataAsync |> Async.RunSynchronously
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With