Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async method to return true or false in a Task

I know that an async method can only return void or Task. I have read similar methods for Exception handling inside async methods. And I'm new to async programming so I am looking for a straightforward solution.

My async method runs an Sql query. If the query was ok, it should notify the caller with a Boolean true and otherwise a false. My method is currently a void so I have no way of knowing.

private async void RefreshContacts()
{
    Task refresh = Task.Run(() =>
    {
        try
        {
            // run the query
        }
        catch { }
    }
    );
    await refresh;           
}

I simply would like to change async to Task so that in my catch statement the method will return a false and otherwise a true.

like image 575
disasterkid Avatar asked Jul 20 '15 09:07

disasterkid


People also ask

Can async method have return value?

Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task<TResult>, for an async method that returns a value. void , for an event handler.

What does an async method return?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.

How do I return a task without await?

C# Language Async-Await Returning a Task without awaitThere is only one asynchronous call inside the method. The asynchronous call is at the end of the method. Catching/handling exception that may happen within the Task is not necessary.

Does await return a task?

An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created.


Video Answer


2 Answers

It sounds like you just need to return a Task<bool> then:

private async Task<bool> RefreshContactsAsync()
{
    try
    {
        ...
    }
    catch // TODO: Catch more specific exceptions
    {
        return false;
    }
    ...
    return true;
}

Personally I would not catch the exception, instead letting the caller check the task for a faulted state, but that's a different matter.

like image 166
Jon Skeet Avatar answered Oct 02 '22 16:10

Jon Skeet


Change the method signature to Task<bool>. Then if your method is declared as async you can simple return a bool value. But as jon skeet said there are other, possibly better ways, to handle your szenario

 private async Task<bool> RefreshContacts()
    {
        Task refresh = Task.Run(() =>
        {
            try
            {
                // run the query
                      return true;
        }
        catch { return false;}      
}

PS: Another common issue you will possibly have is if you have a method without async. Then you can return Task.FromResult(true) like this:

 private Task<bool> RefreshContacts()
 {
     ....
    return Task.FromResult(true)
 }
like image 20
Boas Enkler Avatar answered Oct 02 '22 17:10

Boas Enkler