<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 11 Sep 2017, at 00:10, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Not really certain what async/await adds, using this library (note self promotion) which is built on top of GCD:<div class=""><br class=""></div><div class=""><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><a href="https://github.com/hlovatt/Concurrency-Utilities" class="">https://github.com/hlovatt/Concurrency-Utilities</a><br class=""></div></blockquote><div class=""><br class=""></div><div class="">You can write:</div><div class=""><blockquote type="cite" style="font-size:12.8px" class=""><div style="word-wrap:break-word" class=""><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(186,45,162)" class="">func</span>&nbsp;doit() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><font color="#500050" class="">&nbsp; &nbsp; &nbsp; &nbsp; </font><font color="#703daa" class="">AsynchronousFuture</font><font color="#500050" class="">&nbsp;{ // Executes in background and therefore does not block main</font></div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:rgb(186,45,162)" class="">let</span>&nbsp;dataResource&nbsp; =&nbsp;<span style="text-decoration-line:underline" class="">loadWebResource</span>(<span style="color:rgb(209,47,27)" class="">"dataprofile.<wbr class="">txt"</span>) // Returns a future and therefore runs concurrently in background.</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:rgb(186,45,162)" class="">let</span>&nbsp;imageResource =&nbsp;<span style="text-decoration-line:underline" class="">loadWebResource</span>(<span style="color:rgb(209,47,27)" class="">"imagedata.<wbr class="">dat"</span>) // Future therefore concurrent.</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:rgb(186,45,162)" class="">let</span>&nbsp;imageTmp&nbsp; &nbsp; &nbsp; =&nbsp;<span style="text-decoration-line:underline" class="">decodeImage</span>(dataResource.get ?? defaultText, imageResource.get ?? defaultData) // Handles errors with defaults easily, including timeout.</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:rgb(186,45,162)" class="">let</span>&nbsp;imageResult &nbsp; =&nbsp;<span style="text-decoration-line:underline" class="">dewarpAndCleanupImage</span>(<wbr class="">imageTmp)</div></div></blockquote><blockquote type="cite" style="font-size:12.8px" class=""><div style="word-wrap:break-word" class=""><div style="color:rgb(112,61,170);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Thread.executeOnMain&nbsp;{</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:rgb(186,45,162)" class="">self</span>.imageResult = imageResult</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; }</div></div></blockquote></div><div class="">So why bother with async/await?</div></div></div></div></blockquote><br class=""></div><div>Because async/await serve a different (but related) purpose than background threads. Remember that concurrency is not parallelism: running in parallel in the background is a useful construction, but not the *only* construction.</div><div><br class=""></div><div>If you have multiple tasks you’d like to kick off in parallel, async/await can support that by providing some kind of awaitable Future object. You’d then call multiple functions that return those futures and then await on the completion of all of the Futures (Future.gatherResults is a nice function name).</div><div><br class=""></div><div>However, async/await also allows you to more directly handle the notion that you are making a single blocking call that you need the result of before your computation can complete. For example, if you only had to load *one* web resource instead of two, your code with async/await would look exactly like synchronous code with the word ‘await’ scattered around:</div><div><br class=""></div><div>func doit() async throws {</div><div>&nbsp; &nbsp; let dataResource = await loadWebResource(“dataprofile.txt”)</div><div>&nbsp; &nbsp; let imageTmp = decodeImage(dataResource)</div><div>&nbsp; &nbsp; return dewarpAndCleanupImage(imageTmp)</div><div>}</div><div><br class=""></div><div>At this point scheduling this function becomes the calling programmer’s concern. It also allows multiple calls to doit() to be interleaved without deferring them to background threads and relying on the OS scheduler to schedule them appropriately. This is very useful on server-side applications that do not want to be dispatching large numbers of threads, and that are likely already running an I/O select() loop that can handle the most common cause of awaits (namely, please do this I/O).</div><div><br class=""></div><div>For functions that are computationally heavy, no problem: most languages provide built-in support for scheduling async functions into threadpools and providing a Future that completes on the completion of the background task.</div><div><br class=""></div><div>I guess the TL;DR here is that async/await allows you to have concurrency with or without parallelism, while using thread pools means you can only have concurrency with parallelism.</div><div><br class=""></div><div>Cory</div></body></html>