Is there a way to get rid of the Task type on an object?
I am building my first full stack app where I am building a .NET core website and an API for it to make queries to the database.
I am having trouble converting the API returns into the proper formats that I need for the front end. I can't figure out how to convert Task<IEnumerable<Appetizer>> into List<Appetizer>. I've tried a few different solutions and no luck so far.
14 Replies
getAllAppsFromApi
method even compiling? DeserializeObject
doesn't return a Task
Step one:
Task
s should be await
ed
No exceptions
Also, why use Newtonsoft?
JSON serialization just comes with the framework nowadays
Also also, method names are PascalCase
not camelCase
totally forgot entirely how async methods work whoops
But yeah, await a
Task
to get its resultAlso don't create an
HttpClient
inside the service, inject an IHttpClientFactory
into the serviceYeah I just learned about the clientfactory now and once I get this working I am going to implement that. I've tried doing and it errors within the getAppetizers() method (will change the method names to pascal case also). I'm guessing getAppetizers() needs to be async as well?
1. If a method
await
s anything, it has to be async
2. If a method is async
it has to return Task
or Task<T>
3. If a method is async
it has to be await
ed
Them's the rules
So, yes, the GetAppetizers method has to be async
as well
And has to return Task<LIst<Appetizer>>
How does this look outside of not using IHttpClientFactory:
GetAppetizerList is what is called externally.
I don't see you
await
ing GetAppetizers()
So even if you don't return anything it still needs awaited?
Yes
Angius
1. If a method
await
s anything, it has to be async
2. If a method is async
it has to return Task
or Task<T>
3. If a method is async
it has to be await
edQuoted by
<@85903769203642368> from #Is there a way to get rid of the Task type on an object? (click here)
React with ❌ to remove this embed.
Also, it doesn't not return anything
It returns a
Task
And a Task
has to be await
edI guess I am struggling with how to go about getting it into list form for the front end. I went ahead and added the front end where I am trying to display the list. Basically I want to populate with what the API returns.
GetAppetizerList
is asynchronous
So it needs to be awaited
You can use OnInitializedAsync()