ASP.NET Core Web API .NET 8 2024 - 9. Async/Await

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so unfortunately a lot of the code that you write is going to be incredibly slow no matter what you do you could throw Hardware at it you could do all types of performance tricks but database calls and network calls are slow by Nature they are literal turtles in the world of computing and the reason that this occurs is because you have to reach into an outside system when you make a database call in your code that database is not exist in your code you have to go outside and the same goes for a network request when you make a network request you have to go thousands and thousands of miles to a server to a router somewhere in order to actually be able to get that request and get it back and in the grand scheme of computing these are some of the slowest actions that can exist but we have a secret weapon and that is async and the concept of async as we know it today was actually invented by C but it was quickly stolen by all of the other languages which I totally understand because it actually was a really great idea so let's just talk about it from a 10,000 ft overview synchronous this is slow code what's going to happen is that the code will now be fast because it can go outside of the actual scope what asynchronous code really does is it allows us to execute multiple pieces of code at one time while slow code is being executed in our stock controller we can actually execute code in our comment controller and our stock controller at the same time and asynchronous code is literally the backbone of why servers are so fast today but the beauty of c and the beauty ofet is that you can quickly add async to almost any piece of code watch this so if you want to make a function async the first thing that you do is you just add async to it it now async isn't actually doing anything this word async is literally just for you the developer when you add async to a function there is nothing else going on another thing that we need to do is we need to add this thing called a task a task is just a return type and this is going to give us the return type in the form of a task which is basically a fancy wrapper for our object you see when we actually get done with an async action we have to return something even if the function has not returned even if the database call has not returned we need to actually return something and that thing being returned is a task whether something is there or not the next thing that we need to do is we need to actually add a weight now a weight is just a fancy word for make this acing and if we add a weight to our get stock from the database remember that we don't want to actually make code that is not going outside the system async what's going to happen is that this is going to perform async code just right here and it's not going to perform async code anywhere else and when we actually run this function this code right here is not going to be async this code right here is not going to be async this code right here is not going to be async the only code that's going to be running async is what you put a weit and when you add a weight to a piece of code net is going to add what's called a co- routine and a code routine basically means that you are going to get your code returned to you in the form of a yield and this will allow the code to interrupt any other process that's going on quickly return or then you can quickly go on to your next piece of code and execute the rest of the code but that's a lot so let's go ahead let's hop inside vs code and let's start switching over a lot of the code that we wrote to async so now we are inside of our controller and we need to go ahead and start turning this boring synchronous code into fast cool async code and as always what we do is we go ahead we slap on the async keyword remember async doesn't actually do anything underneath the hood it's there for you to be able to identify asyn orness functions because once you actually make code async it's going to be a lot harder to debug and you need to be able to spot when the await keyword is present but we will make an await keyword just a second after we add the return t task the return task is the return type that is going to be returned at the end of the function and in our case our ey action result is what's going to be returned but we need to wrap it in a task because technically when this returns something has to be there it may not have actually returned this await may not have actually succeeded and we got something from our database but at the end of the day the compiler still needs a value there to be able to hold while it runs other stuff and once this task gets completed it's going to use a yield to quickly get the value back where your code will resume but let's go ahead we also need to finish here we need to make sure that our actual asynchronous code this is actually asynchronous code nothing else in this function is actually asynchronous this is the only piece of asynchronous code because it has no weight so what we need to do is we need to take this previous two list this boring two list and we need to make it a now we could make this select async right here but I think it's easier just to kind of break this apart go down here and what I'm going to do is just make a bar uh stock dto I'm going to turn this into a stock dto and we are going to wait till this returns and then we are going to do the select so after this gets done running then we will for sure have our stock dto down here where we can then manipulate it in return that was a lot the next functions are going to be a little bit easier so let's go down here same exact thing we're going to go async go ahead wrap this in a task go down here and we need to identify because we're going to actually go to the database we need to make this in await and we need to make this a find async so we're going to go find async looking good not really much else to do here that was a pretty much a gimme so next async go ahead wrap this in a task then go here go task and anything remember any anything that's going to the database we want that await so we're going to go await right here go await right here and you're going to also get red squiggly lines because we need to make this async and luckily for us NTI framework has all the asynchronous code built directly in and we don't have to actually worry about any of it so go down here go async go task go action result we need to look for anything that's going to the database so we got database code right here we're going to go first your D default async it's going to go ahead bring that in and need to make this in a we because we're hitting the database and we also need to make this async lastly delete is going to be a gimme also need to okay I almost thought I forgot to put the async right here okay so the delete is going to be pretty much a gimme it's going to be very easy we just have to wrap it in a task so go here we're going to go here first your default a sync this is actually super easy except for the delete the delete gets a little crazy do not add in a weight to the delete because remove is not an asynchronous function I don't know why that is if you do know why please leave a comment down in the description and please Al lighten us because I searched everywhere and could not find the reason so that looks pretty much good to go um I'm going to go down here go ahead start running this thing so net watch run go ahead run it so let's go ahead and test everything I'm going to go in here I'm going to do a get and see here everything looks good getting the same values back that we got before going to go ahead add another post like this let's go back up test it real quick and it looks a lot faster as you can tell so our string did show up let's go ahead down here I'm going to grab this and going to see here we go ahead do our get by D go ahead pass that in here looking good let's do a put try it out go ahead pass that in there and I'll change the divid the purchase to 90 looking good and then let's go into here and let's go ahead and delete it and with that we'll make sure that everything is working correctly and we have a 204 everything is looking good so after this we're going to go ahead move on to repository hope that you guys enjoyed this if you did Smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 2,073
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: Ao2-wILGkPU
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Tue Jan 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.