Marking F# method with `async` modifier

Viewed 56

I am trying to implement a method in F# that would be used from C# in async .. await manner. Here is the stub:

[<Extension>]
static member DoWorkAsync(value : MyValueType): Task<MyOtherType> =
    task {
        // do something and return
    }

However, when I consume this method from C# I get

The 'await' expression can only be used in a method or lambda marked with the 'async' modifier

What would be the way to mark F# method with async modifier?

1 Answers

It's hard to tell without seeing the C# code, but is sounds like you're doing something like:

public Task<MyOtherType> FooAsync(MyValueType v)
{
    var v = await DoWorkAsync(v);
    return new MyOtherType(v); 
}

while the following would work:


public async Task<MyOtherType> FooAsync(MyValueType v)
//     ^^^^^
{
    var v = await DoWorkAsync(v);
    return new MyOtherType(v); 
}
Related