DEV Community

Discussion on: Episode 010 - Async all the things - ASP.NET Core: From 0 to overkill

Collapse
 
joaofbantunes profile image
João Antunes

Not sure I get the point on R6. It would follow the same path as all the others before it, but maybe you can detail more what's your doubt?

As for cancellation tokens, ideally almost all async methods should get them.
Two possible scenarios where they are useful are:

  1. When the result is no longer required
  2. When you, for some reason want to abort the operation

An example of 1, is for example if you get a request from the browser, but the user goes away before it is completed, the connection being closed triggers the token cancellation, so the application can cancel what it's doing, as it'll be no longer useful. You can see an example of this in action in this post.

As for 2, imagine you have an application running in the background doing things, but then it gets signaled to stop (e.g. stop a Windows service). This signal can trigger a cancellation token and the application can abort what it's doing, so it can stop gracefully.

Hope this makes sense!

Collapse
 
dyagzy profile image
dyagzy

Yes, your explanation above makes more sense to me now. Now I know where and when to use the CancelationToken. I will come here for more questions when I move to other episodes in the series. Thank you once again for the knowledge shared and time.

Thread Thread
 
joaofbantunes profile image
João Antunes

Great!