{"id":109961,"date":"2024-07-09T07:00:00","date_gmt":"2024-07-09T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109961"},"modified":"2024-06-24T10:38:17","modified_gmt":"2024-06-24T17:38:17","slug":"20240709-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240709-00\/?p=109961","title":{"rendered":"Creating an already-completed asynchonous activity in C++\/WinRT, part 1"},"content":{"rendered":"<p>When working with asynchronous code, you may need to create an asynchronous activity that has already completed, because you already know the answer. For example, you may be implementing a method whose signature is<\/p>\n<pre>IAsyncOperation&lt;int&gt; ComputeAsync();\r\n<\/pre>\n<p>but you already have the result and don&#8217;t need to compute it. How can you return an <code>IAsyncOperation&lt;int&gt;<\/code> that represents the already-computed result?<\/p>\n<p>C# has <code>Task.FromResult()<\/code> and <code>Task.CompletedTask<\/code>. JavaScript has <code>Promise.resolve()<\/code>. The Parallel Patterns Library (PPL) has <code>task_<wbr \/>from_<wbr \/>result()<\/code>. What about C++\/WinRT?<\/p>\n<p>The simplest way is to just <code>co_return<\/code> the result into a coroutine.<\/p>\n<pre>winrt::Windows::Foundation::IAsyncOperation&lt;int&gt;\r\n    ComputeAsync()\r\n{\r\n    co_return 42;\r\n}\r\n<\/pre>\n<p>Similarly, C# has <code>Task.FromException()<\/code>, JavaScript has <code>Promise.reject()<\/code>, and PPL has <code>task_<wbr \/>from_<wbr \/>exception()<\/code>. The simple C++\/WinRT version is to throw the exception from the coroutine.<\/p>\n<p>But wait, this doesn&#8217;t do what you think:<\/p>\n<pre>winrt::Windows::Foundation::IAsyncOperation&lt;int&gt;\r\n    ComputeAsync()\r\n{\r\n    throw winrt::hresult_access_denied();\r\n}\r\n<\/pre>\n<p>There is no <code>co_await<\/code> or <code>co_return<\/code> statement in the function body, so this is not a coroutine: Instead of returning a failed coroutine, this function fails to return a coroutine! When you call it, it throws an exception.<\/p>\n<p>We&#8217;ll look at ways of making this a coroutine next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The simplest version.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-109961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The simplest version.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=109961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=109961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}