<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Cancellation and time out can be built into futures, and async/await can interact with futures. I don’t think we need async/await itself to support either of those.<div class=""><br class=""></div><div class="">Just as a real-world example, C#’s async/await feature doesn’t have built-in timeout or cancellation support, but it’s still easy to handle both of those cases using the tools available. For example, one technique would be this (in C#):</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">var cts = new CancellationTokenSource();</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">cts.CancelAfter(TimeSpan.FromMilliseconds(2500));</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">try {</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>await DoAsync(cts.Token);</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">}</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">catch (OperationCanceledException) {</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Handle cancelled</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">}</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">catch (Exception) {</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Handle other failure</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">}</blockquote><div class=""><br class=""></div>There are other techniques that would let you distinguish between cancellation and timeout as well.<br class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 25, 2017, at 7:06 AM, Cavelle Benjamin 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Disclaimer: not an expert</div><div class=""><br class=""></div><div class="">Question</div><div class="">I didn’t see any where the async is required to time out after a certain time frame. I would think that we would want to specify both on the function declaration side as a default and on the function call side as a customization. That being said, the return time then becomes an optional given the timeout and the calling code would need to unwrap.</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; 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);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">loadWebResource</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">path</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">String</span>) async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Resource
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">decodeImage</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">r1</span>: Resource, <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">r2</span>: Resource) async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">dewarpAndCleanupImage</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">i</span> : Image) async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image

<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">processImageData1</span>() async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image {
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> dataResource  <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">loadWebResource</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>dataprofile.txt<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageResource <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">loadWebResource</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>imagedata.dat<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageTmp      <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">decodeImage</span>(dataResource, imageResource)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageResult   <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">dewarpAndCleanupImage</span>(imageTmp)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">return</span> imageResult
}</pre><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">So the prior code becomes…&nbsp;</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; 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);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">loadWebResource</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">path</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">String</span>) async(timeout: 1000) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Resource?
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">decodeImage</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">r1</span>: Resource, <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">r2</span>: Resource) async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image?
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">dewarpAndCleanupImage</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">_</span> <span class="pl-smi" style="box-sizing: border-box;">i</span> : Image) async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image?

<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">processImageData1</span>() async <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> Image? {
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> dataResource  <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> guard let await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">loadWebResource</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>dataprofile.txt<span class="pl-pds" style="box-sizing: border-box;">”</span></span>) else { // handle timeout }
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageResource <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> guard let await(timeout: 100) <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">loadWebResource</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>imagedata.dat<span class="pl-pds" style="box-sizing: border-box;">”</span></span>) else { // handle timeout }
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageTmp      <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">decodeImage</span>(dataResource, imageResource)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> imageResult   <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">dewarpAndCleanupImage</span>(imageTmp)
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">return</span> imageResult
}</pre><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">Given this structure, the return type of all async’s would be optionals with now 3 return types??</div><div class=""><br class=""></div><div class="">.continuation // suspends and picks back up</div><div class="">.value // these are the values we are looking for</div><div class="">.none // took too long, so you get nothing.</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 2017-Aug -17 (34), at 18:24, Chris Lattner 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 class="">Hi all,<br class=""><br class="">As Ted mentioned in his email, it is great to finally kick off discussions for what concurrency should look like in Swift. &nbsp;This will surely be an epic multi-year journey, but it is more important to find the right design than to get there fast.<br class=""><br class="">I’ve been advocating for a specific model involving async/await and actors for many years now. &nbsp;Handwaving only goes so far, so some folks asked me to write them down to make the discussion more helpful and concrete. &nbsp;While I hope these ideas help push the discussion on concurrency forward, this isn’t in any way meant to cut off other directions: in fact I hope it helps give proponents of other designs a model to follow: a discussion giving extensive rationale, combined with the long term story arc to show that the features fit together.<br class=""><br class="">Anyway, here is the document, I hope it is useful, and I’d love to hear comments and suggestions for improvement:<br class=""><a href="https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782" class="">https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782</a><br class=""><br class="">-Chris<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>