Building async/await from scratch with Stephen Toub
Published by marco on
This is another video from Stephen Toub that is just chock/full of useful information.
At 27:30, they start to discuss about the nomenclature of Task
and how it differs from an Action
. It’s funny that neither of them mentioned that tasks in .NET are called promises pretty much everywhere else (JavaScript, Java, etc.). Some libraries also use the word future. For more information, see Futures and promises (Wikipedia).
As he’s building everything, it is really astonishing to note that Hanselmann has to tell Toub that you can have Visual Studio 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 F12. When he got to the method, he said “Oh, it didn’t implement it,” 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 “trick” to create the method.
At around 52:30, he implemented a try
/catch
to “be a good citizen” and accepted what Copilot had recommended for him, but it didn’t match what he said he was writing. He said “so we always set the task result” but the code that he/Copilot wrote returned from the catch
, 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 1:00:00, he seemed to indicate that what it wrote was OK: i.e., it either sets the exception or it sets the result.
At 56:00, he gets to the point of trying to get to the synchronous calling style supported by await
and builds his own logic. It works, but it still can’t be used with the await
keyword. He quickly implements a TaskAwaiter
and voila! 🧙♂️ it works! His very own implementation of the Task pattern that integrates with the compiler.