Reputation: 610
I am building new rpc api which by rule should be async but if this does not have any IO call today then i can not add await in the async method.
I do not want to make sync API today based on the current requirement as it may change later, later I may want to add IO call in the API which will end up blocking the thread if I make sync API today.
Question: Is there a recommended pattern to create async method without any await call in it?
Upvotes: -2
Views: 33
Reputation: 1063854
If you mean you want the API to support async later, but you don't have any async operations today: that's fine - you can still just return Task
/ Task<T>
and use Task.FromResult
/ Task.FromException
. if you want to avoid the object overhead: ValueTask
/ ValueTask<T>
work similarly, but the caller has some additional demands (specifically: to access the result exactly once).
You could also just suppress the compiler warning about missing await
.
Upvotes: 1