<div dir="ltr">Not really certain what async/await adds, using this library (note self promotion) which is built on top of GCD:<div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><a href="https://github.com/hlovatt/Concurrency-Utilities">https://github.com/hlovatt/Concurrency-Utilities</a><br></div></blockquote><div><br></div><div>You can write:</div><div><blockquote type="cite" style="font-size:12.8px"><div style="word-wrap:break-word"><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(186,45,162)">func</span> doit() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><font color="#500050">        </font><font color="#703daa">AsynchronousFuture</font><font color="#500050"> { // 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">            <span style="color:rgb(186,45,162)">let</span> dataResource  = <span style="text-decoration-line:underline">loadWebResource</span>(<span style="color:rgb(209,47,27)">&quot;dataprofile.<wbr>txt&quot;</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">            <span style="color:rgb(186,45,162)">let</span> imageResource = <span style="text-decoration-line:underline">loadWebResource</span>(<span style="color:rgb(209,47,27)">&quot;imagedata.<wbr>dat&quot;</span>) // Future therefore concurrent.</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(186,45,162)">let</span> imageTmp      = <span style="text-decoration-line:underline">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">            <span style="color:rgb(186,45,162)">let</span> imageResult   = <span style="text-decoration-line:underline">dewarpAndCleanupImage</span>(<wbr>imageTmp)</div></div></blockquote><blockquote type="cite" style="font-size:12.8px"><div style="word-wrap:break-word"><div style="color:rgb(112,61,170);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            Thread.executeOnMain {</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                <span style="color:rgb(186,45,162)">self</span>.imageResult = imageResult</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            }</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</div><div style="color:rgb(80,0,80);margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div></div></blockquote></div><div>So why bother with async/await?</div></div><div><br></div><div>PS I also agree with the comments that there is no point writing the 1st two lines of the example with async and then calling them with await - you might as well write serial code.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">  -- Howard.<br></div></div>
<br><div class="gmail_quote">On 10 September 2017 at 10:33, Wallacy via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is the only part of the proposal that i can&#39;t concur!<br><br>^async^ at call side solve this nicely! And Pierre also showed how common people are doing it wrong! And will make this wrong using Futures too.<br><br>func doit() async {<br>        let dataResource  = async loadWebResource(&quot;dataprofile.<wbr>txt”)<br>        let imageResource = async loadWebResource(&quot;imagedata.<wbr>dat”)<span class=""><br>        let imageTmp = await decodeImage(dataResource, imageResource)<br></span>        self.imageResult = await dewarpAndCleanupImage(<wbr>imageTmp)<br>}<br><br>Anyway, we have time to think about it.<div class="HOEnZb"><div class="h5"><br><br><br><div class="gmail_quote"><div dir="ltr">Em sáb, 9 de set de 2017 às 20:30, David Hart via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On 10 Sep 2017, at 00:40, Kenny Leung via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-1347321129620839407m_7630931665265230838Apple-interchange-newline"><div><div style="word-wrap:break-word"><div>Then isn’t the example functionally equivalent to:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    <span style="color:#ba2da2">func</span> doit() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        <span style="color:#703daa">DispatchQueue</span>.<span style="color:#3e1e81">global</span>().<span style="color:#3e1e81">async</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">            <span style="color:#ba2da2">let</span> dataResource  = <span style="text-decoration:underline">loadWebResource</span>(<span style="color:#d12f1b">&quot;dataprofile.<wbr>txt&quot;</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">            <span style="color:#ba2da2">let</span> imageResource = <span style="text-decoration:underline">loadWebResource</span>(<span style="color:#d12f1b">&quot;imagedata.<wbr>dat&quot;</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">            <span style="color:#ba2da2">let</span> imageTmp      = <span style="text-decoration:underline">decodeImage</span>(dataResource, imageResource)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">            <span style="color:#ba2da2">let</span> imageResult   = <span style="text-decoration:underline">dewarpAndCleanupImage</span>(<wbr>imageTmp)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170);background-color:rgb(255,255,255)"><span>            </span>DispatchQueue<span>.</span>main<span>.</span><span style="color:#3e1e81">async</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">                <span style="color:#ba2da2">self</span>.imageResult = imageResult</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">            }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    }</div></div><div><br></div><div>if all of the API were synchronous? Why wouldn’t we just exhort people to write synchronous API code and continue using libdispatch? What am I missing?</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>There are probably very good optimisations for going asynchronous, but I’m not the right person for that part of the answer.</div><div><br></div><div>But I can give another answer: once we have an async/await pattern, we can build Futures/Promises on top of them and then we can await on multiple asynchronous calls in parallel. But it won’t be a feature of async/await in itself:</div><div><br></div><div>func doit() async {<br><span class="m_-1347321129620839407m_7630931665265230838Apple-tab-span" style="white-space:pre-wrap">        </span>let dataResource  = Future({ loadWebResource(&quot;<wbr>dataprofile.txt”) })<br><span class="m_-1347321129620839407m_7630931665265230838Apple-tab-span" style="white-space:pre-wrap">        </span>let imageResource = Future({ loadWebResource(&quot;<wbr>imagedata.dat”) })</div><div><span class="m_-1347321129620839407m_7630931665265230838Apple-tab-span" style="white-space:pre-wrap">        </span>let imageTmp = await decodeImage(<wbr>dataResource.get, imageResource.get)<br>        self.imageResult = await <wbr>dewarpAndCleanupImage(<wbr>imageTmp)<br>}</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div>-Kenny</div><div><br></div><br><div><blockquote type="cite"><div>On Sep 8, 2017, at 2:33 PM, David Hart &lt;<a href="mailto:david@hartbit.com" target="_blank">david@hartbit.com</a>&gt; wrote:</div><br class="m_-1347321129620839407m_7630931665265230838Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 8 Sep 2017, at 20:34, Kenny Leung via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-1347321129620839407m_7630931665265230838Apple-interchange-newline"><div><div style="word-wrap:break-word">Hi All.<div><br></div><div>A point of clarification in this example:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;color:rgb(36,41,46);font-variant-ligatures:normal"><span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">loadWebResource</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">_</span> <span class="m_-1347321129620839407m_7630931665265230838pl-smi" style="box-sizing:border-box">path</span>: <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>) async <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> Resource
<span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">decodeImage</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">_</span> <span class="m_-1347321129620839407m_7630931665265230838pl-smi" style="box-sizing:border-box">r1</span>: Resource, <span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">_</span> <span class="m_-1347321129620839407m_7630931665265230838pl-smi" style="box-sizing:border-box">r2</span>: Resource) async <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> Image
<span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dewarpAndCleanupImage</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">_</span> <span class="m_-1347321129620839407m_7630931665265230838pl-smi" style="box-sizing:border-box">i</span> : Image) async <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> Image

<span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_-1347321129620839407m_7630931665265230838pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">processImageData1</span>() async <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> Image {
    <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> dataResource  <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span>dataprofile.<wbr>txt<span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span></span>)
    <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageResource <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span>imagedata.dat<span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box"><wbr>&quot;</span></span>)
    <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageTmp      <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">decodeImage</span>(dataResource, imageResource)
    <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageResult   <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">dewarpAndCleanupImage</span>(<wbr>imageTmp)
    <span class="m_-1347321129620839407m_7630931665265230838pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> imageResult
}</pre><div><br></div></div><div>Do these:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;color:rgb(36,41,46);font-variant-ligatures:normal">await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span>dataprofile.<wbr>txt<span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span></span>)</pre><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;color:rgb(36,41,46);font-variant-ligatures:normal">await <span class="m_-1347321129620839407m_7630931665265230838pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="m_-1347321129620839407m_7630931665265230838pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box">&quot;</span>imagedata.dat<span class="m_-1347321129620839407m_7630931665265230838pl-pds" style="box-sizing:border-box"><wbr>&quot;</span></span>)</pre><div><br></div></div></div><div>happen in in parallel?</div></div></div></blockquote><div><br></div><div>They don’t happen in parallel.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div>If so, how can I make the second one wait on the first one? If not, how can I make them go in parallel?</div><div><br></div><div>Thanks!</div><div><br></div><div>-Kenny</div><div><br></div></div>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div></div>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</blockquote></div>
</div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>