How to convert Task to Task<Unit> with Language-Ext

Viewed 781

Binding tasks together with https://github.com/louthy/language-ext requires tasks with return type (Task<>). Tasks without return type should therefore be converted into Task<Unit>.

Does anybody know a compact (expression only) way to convert Task to Task<Unit> in C# -- with (or without) using Language-Ext?

In other words: Is there something like fun(...) for Task?

1 Answers

I'm not able to test this right now, but it should do what you want.

public static class TaskExtensions
{
    public static async Task<Unit> ToUnit(this Task task)
    {
        await task;
        return unit;
    }
}

Then call:

task.ToUnit();

On your untyped tasks. I'll probably add this to lang-ext at some point.

Related