This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

Building async/await from scratch with Stephen Toub

Description

This is another video from Stephen Toub that is just chock/full of useful information. <media href="https://www.youtube.com/watch?v=R-z2Hv-7nxk" src="https://www.youtube.com/v/R-z2Hv-7nxk" source="YouTube" caption="Writing async/await from scratch in C# with Stephen Toub" author="dotnet" width="560px"> At <b>27:30</b>, they start to discuss about the nomenclature of <c>Task</c> and how it differs from an <c>Action</c>. It's funny that neither of them mentioned that <i>tasks</i> in .NET are called <i>promises</i> pretty much everywhere else (JavaScript, Java, etc.). Some libraries also use the word <i>future</i>. For more information, see <a href="https://en.wikipedia.org/wiki/Futures_and_promises" source="Wikipedia">Futures and promises</a>. As he's building everything, it is really astonishing to note that Hanselmann has to tell Toub that you can have <i>Visual Studio</i> generate methods for you. How does he not know that? When he did it, he then used the mouse to select "Find References" from the shortcut menu instead of just pressing <kbd>F12</kbd>. When he got to the method, he said <iq>Oh, it didn't implement it,</iq> as if disappointed that Copilot hadn't botshit a version in there for him. He was going to write it himself anyway, but it was telling that he's gotten so accustomed to Copilot just filling in implementation. A little while later, he's learned the new tool, telling Hanselmann that he's going to use his <iq>trick</iq> to create the method. At around <b>52:30</b>, he implemented a <c>try</c>/<c>catch</c> to <iq>be a good citizen</iq> and accepted what Copilot had recommended for him, but it didn't match <i>what he said he was writing</i>. He said <iq>so we always set the task result</iq> but the code that he/Copilot wrote <i>returned</i> from the <c>catch</c>, which means that the task result isn't going to be set when there is an exception. Now I don't know which one he meant: what he said he wanted to write (did he misspeak?) or what he actually wrote (which Copilot wrote for him and he might have automatically accepted). Since he has no tests whatsoever, this is exactly the kind of subtle bug that might go undetected for quite a while, as it's in the exception-handling code. It might also be quite difficult to diagnose. When he wrote the exact same thing again at <b>1:00:00</b>, he seemed to indicate that what it wrote was OK: i.e., it either sets the exception or it sets the result. At <b>56:00</b>, he gets to the point of trying to get to the synchronous calling style supported by <c>await</c> and builds his own logic. It works, but it still can't be used with the <c>await</c> keyword. He quickly implements a <c>TaskAwaiter</c> and voila! 🧙‍♂️ it works! His very own implementation of the <i>Task</i> pattern that integrates with the compiler.