0

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.

Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • What you're doing seems fairly fine? the question is, does the consumer of this code need to know that `DoTheWork()` happened or not. if the answer is 'no' then this is fine – zaitsman Sep 09 '20 at 00:35
  • yup. Or you can return `Task.FromResult()` – zaitsman Sep 09 '20 at 00:45
  • I've removed request for external links from the post... Please [edit] more to clarify why code shown in the post does not work for you. – Alexei Levenkov Sep 09 '20 at 00:51
  • Also "the caller did not care whether DoTheWork happens" comment hints that maybe you actually don't want `DoTheWork` to be called or maybe you don't want to wait for synchronous part of `DoTheWork` to finish in the calling code - some clarification of that would help too. – Alexei Levenkov Sep 09 '20 at 00:54
  • @AndySong I posted answer to *my interpretation* of the question, with two answers it looks like most reasonable ways of reading the question is covered. Feel free to edit the question to match the answer. – Alexei Levenkov Sep 09 '20 at 01:14
  • @AndySong it started to collect downvotes - and since I have no idea why I flagged it to be deleted instead. – Alexei Levenkov Sep 09 '20 at 23:45
  • "Your code is perfectly fine. But if you really want change - https://stackoverflow.com/questions/35201881/should-i-use-async-await-for-every-method-that-returns-a-task". I can see how that is really opinion based answer (and explaining difference between using/not using `async` is not my favorite topic... but you can dig into that try searching for differences in exception handling) – Alexei Levenkov Sep 10 '20 at 04:31
  • posted in the chat. – Alexei Levenkov Sep 10 '20 at 04:39

1 Answers1

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://learn.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.

John R.
  • 170
  • 8