Return either an in-memory (cached) collection, or a task from an async await response

Viewed 1257

I have a scenario where my service class communicates to an API and populates a list. This method is

 public async Task<List<T>> Foo()

and in this method I am doing an async await to retrieve data from the API and deserialize to a list.

The thing is, I'd like to do something like this:

if (list is cached in memory)
   //serve data from the in memory list
else
   //await get data from the API

But the return type in the first part of the if statement is List<T> whereas the return type of the second part is Task<List<T>>

How can I do this? Can I just have my in-memory list be a Task? (Maybe I can wrap the List in a task)?

Thank you all!

4 Answers
Related