how to handle async method return both awaitable and non-awaitable

What I want to do is that in an async method when it’s under some conditions I do not want to do anything and when it’s not I want to do the await code. (I do not want to throw an exception either because it’s ok to not to do anything.) How should I do this? or what’s the better of doing it?

So I have the code below:

public async Task<JObject> Process()
{
     //if the _id is null I just do not want to do anything 
     //I do not want to throw an exception either as it's ok to not to do anything.
     // I am not sure what is the recommended way to do this so I am just returning `new JObject()`
     // so It compiles.

     if(_id == null ) return new JObject();

     return await DoTheWork();
}

I found this answer is very useful for me, I just post here for reference.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Edit: My original answer did not address the post’s real question.

There’s another post related to this about returning null versus an empty object: Should functions return null or an empty object?

Returning null is usually the best idea if you intend to indicate that
no data is available.

An empty object implies data has been returned, whereas returning null
clearly indicates that nothing has been returned.

Additionally, returning a null will result in a null exception if you
attempt to access members in the object, which can be useful for
highlighting buggy code – attempting to access a member of nothing
makes no sense. Accessing members of an empty object will not fail
meaning bugs can go undiscovered.

However, if it was ever not ok for _id to be null then throwing an exception would probably be the better solution since it would help you gracefully handle bugs in the logic and also allow you to add logging.

Microsoft also has some great documentation on on asynchronous programming in general. I would take a look at this article here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/task-asynchronous-programming-model, particularly this part:

If you specify that a method is an async method by using the async modifier, you enable the following two capabilities.

  • The marked async method can use await to designate suspension points. The await operator tells the compiler that the async method can’t continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.

    The suspension of an async method at an await expression doesn’t constitute an exit from the method, and finally blocks don’t run.
  • The marked async method can itself be awaited by methods that call it.

An async method typically contains one or more occurrences of an await operator, but the absence of await expressions doesn’t cause a compiler error. If an async method doesn’t use an await operator to mark a suspension point, the method executes as a synchronous method does, despite the async modifier. The compiler issues a warning for such methods.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x