Why you do not use await in queries #6978
-
Hello i started to read the documentation and I noticed this piece of code in the examples, public class Query so why you do not use await collection.Find(x => x.Id == id).FirstOrDefaultAsync(); ?? And general beyond this example I want to call any async service inside the query. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The return value from As a result, an async state machine is not compiled around it. However, you can still use |
Beta Was this translation helpful? Give feedback.
-
Thank you very much! |
Beta Was this translation helpful? Give feedback.
The return value from
FirstOrDefaultAsync()
is of typeTask<Book?>
, which means you do not need to await it since it matches the expected return type.As a result, an async state machine is not compiled around it.
However, you can still use
async
andawait
if you prefer.The difference is negligible, and Hot Chocolate understands both approaches.