## Why is this an issue?

Returning `Nothing` from a non-`Async` `Task`/`Task(Of TResult)` procedure will cause a `NullReferenceException` at runtime if the procedure is awaited. This problem can be avoided by returning `Task.CompletedTask` or `Task.FromResult(Of TResult)(Nothing)` respectively.

    Public Function DoFooAsync() As Task
        Return Nothing            ' Noncompliant: Causes a NullReferenceException if awaited.
    End Function

    Public Async Function Main() As Task
        Await DoFooAsync()        ' NullReferenceException
    End Function

## How to fix it

Instead of `Nothing` `Task.CompletedTask` or `Task.FromResult(Of TResult)(Nothing)` should be returned.

### Code examples

A `Task` returning procedure can be fixed like so:

#### Noncompliant code example

    Public Function DoFooAsync() As Task
        Return Nothing            ' Noncompliant: Causes a NullReferenceException if awaited.
    End Function

#### Compliant solution

    Public Function DoFooAsync() As Task
        Return Task.CompletedTask ' Compliant: Method can be awaited.
    End Function

A `Task(Of TResult)` returning procedure can be fixed like so:

#### Noncompliant code example

    Public Function GetFooAsync() As Task(Of Object)
        Return Nothing                             ' Noncompliant: Causes a NullReferenceException if awaited.
    End Function

#### Compliant solution

    Public Function GetFooAsync() As Task(Of Object)
        Return Task.FromResult(Of Object)(Nothing) ' Compliant: Method can be awaited.
    End Function

## Resources

### Documentation

- Microsoft Learn - `Task.CompletedTask` Property
- Microsoft Learn - `Task.FromResult<TResult>(TResult)` Method

### Articles & blog posts

- StackOverflow - Answer by Stephen Cleary for Is it better to return an empty task or null?
- StackOverflow - Answer by Stephen Cleary for Best way to handle null task inside async method?