Task.FromResult() vs. Task.Run()

If your method is synchronous you shouldn’t return a Task to begin with. Just create a traditional synchronous method.

If for some reason that’s not possible (for example, you implement some async interface) returning a completed task using Task.FromResult or even better in this case Task.CompletedTask (added in .NET 4.6) is much better than using Task.Run in the implementation:

public virtual Task CreateAsync(TUser user)
    // ...
    return Task.CompletedTask;

If the consumer of your API cares strongly about the Task-returning method not running synchronously they can use Task.Run themselves to make sure.

You should keep in mind that async methods may have a considerable synchronous part (the part before the first await) even if they do eventually continue asynchronously. You can’t assume async methods return a Task immediately anyway.

Leave a Comment