8 await async mistakes that you SHOULD avoid in .NET

preview_player
Показать описание

Hello everybody I'm Nick and in this video I wanna talk real world mistakes that you might encounter regarding await async. It's an interesting topic that many people tend to get wrong and this comes down to how Microsoft handled the original rollout of the feature. In this video I will go through 8 real world scenarios of how it can be done wrong and talk about how we can fix them.

Timestamps:
0:00 - Intoduction
1:19 - Once async always async
2:46 - Async void is BAD
6:05 - Prefer Task.FromResult over Task.Run
8:00 - Avoid .Result and .Wait
9:37 - Prefer await over ContinueWith
11:19 - Always pass the CancellationToken
15:16 - Prefer async Task over Task
17:11 - Don't sync over async in constructors

Don't forget to comment, like and subscribe :)

Social Media:

#dotnet #await #async
Рекомендации по теме
Комментарии
Автор

Thank's for the mention :) also a very needed video on youtube, well done. One thing I could reccomend is to increase the font on Rider's menu's

RawCoding
Автор

Dude the cancelation token at endpoint lvl was awesome.

MrBarralex
Автор

"You should avoid async void". Well that's where the word "avoid" from.

protaties
Автор

Something I did in a project when needing a resource loaded within a constructor was to build something that let me lazily load the data within an asynchronous context and await when I need it. It was just a small class built on top of Lazy<T> that I called AsyncLazy<T>. I can still pass in my interface that does the asynchronous call but stuff it inside of AsyncLazy<T> and whenever I need that data I can just do (await _settingsLazy).<Whatever>;
Plus, in my opinion, I like the way it reads. It tells whoever is reading that piece of code that the data is lazily loaded, cached after the first call, and is done within an asynchronous context.

logank.
Автор

Love the information shared ! Thank you Nick. Learned a lot from the "Always pass the CancellationToken" and the "Don't sync over async in constructors" !

MINDoSOFT
Автор

Hi Nick, thank you for your hard work and knowledge sharing here. It would be great if you elaborate about best practices on error handling. It is one on the wide and most useful topics. There are a lot of videos and resources on the internet, but still some of the ideas behind them are not clear for me. Thanks.

Reza-ztsx
Автор

About the last case, when you need to resolve service from DI, there is an article at msdn 'Dependency injection guidelines' with anti-pattern examples where is shown how you can deadlock your thread with an async factory.

metlic
Автор

Really appreciate the timestamps in the description. Keep up the good work

fahtihi
Автор

Brilliant gotchas picked.
Very concise and understandable demo sequencing.
You are a very good teacher, sir.

JohnWilliams-gyyc
Автор

Awesome Nick. Clear and easy to understand. Keep simple. Thanks

leandrowitzke
Автор

An excellent video going over Async. Lots of great information here!

BlazarVision
Автор

Ah man, I should update my personal project... Thanks alot Nick.

victorcomposes
Автор

Very nice indeed, especially the cancellation token demo 👍.
Keep posting more videos about this topic 👏👏

MohamedOmar-zwbq
Автор

I really liked the way to pass a factory to a constructor that you need to make async. Very enlighting. Thank you Nick!

Mauricio.Solorzano
Автор

Great content Nick. You should have also shared some insight on 'ConfigureAwait' 😉

sujithacharya
Автор

Thank you so much for this. Best practices vids are the best.

qwerty
Автор

I'm more curious about the async await mistakes I should be making

casperes
Автор

Hi Nick, can you explain to me, why should I write "return await new ValueTask<int>(numberToAdd*2);", if I could also write "return numberToAdd*2;" directly. To explain more, we are in an asyc method, so return a value directly is automatically wrapped in a Task (or in this case a ValueTask). So what is the benefit of this additional await?

martinprohn
Автор

Amazing! Applying into project right now! 🙏😁

marcelocarvalho
Автор

Thank you for this video, now I know that async/await in our codebase is really messy.

codingwithgyver