❔ Are TryAsync methods acceptable?
I've just been refactoring some implementations of a method I have to go from
T Load()
to bool TryLoad(out T? result)
, which as far as I'm concerned is not an anti pattern. I was about to do the same for my Task<T> LoadAsync
methods but am wondering if this is bad practice and having bool TryLoadAsync(out Task<T?> result)
may have some unforeseen consequences?8 Replies
I mean, if it has to be async, it has to be async
It doesn't have to be, I've got an ILoad and an IAsyncLoad that some classes implement both of so down the line I have the option. It also doesn't have to be a try method, but if there's no issues I think it's a good idea
If a method doesn't do any
await
ing inside... why do you want an async
version of it?For context, this is the class. It's a FIleLoader where the user has the option to load a file async or synchronously.
Try...Async usually breaks once you realize that ref, in and out parameters are not allowed for async methods
Also, not sure how it would work with this delegate
That sometimes is async, sometimes isn't
Hmm yeah perhaps this is too problematic, I might just drop the async approach since atm it's a single threaded console app
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.