<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=""><blockquote type="cite" class=""><span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; font-size: 14px;" class="">I'm not sure why it's understood as all or nothing, AFAICU, await is just a way to block the current queue and wait for a result from an async function.</span><br class=""><div class=""><div style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; font-size: 14px;" class="">beginAsync is a way to make sure we don't block the current execution queue.</div></div></blockquote><div class=""></div><div class=""><br class=""></div>Await does <i class="">not</i> block the current queue. You can imagine that it gathers up the rest of the function after the ‘await’ line and moves it into a callback. That is, it turns this:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">let x = await somethingThatTakesALongTime()</font></div><div class=""><font face="Menlo" class="">print(“Congratulations, we got x”)</font></div><div class=""><font face="Menlo" class="">print(“x: \(x)”)</font></div></blockquote><div class=""><br class=""></div>into something like this:<br class=""><div class=""><div class=""><div class=""><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><div class=""><div class=""><font face="Menlo" class="">somethingThatTakesALongTime(completion: { (x) in</font></div><div class=""><span style="font-family: Menlo;" class=""> print(“Congratulations, we got x”)</span></div></div></div></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo;" class=""> print(“x: \(x)”)</span><br class=""><font face="Menlo" class="">})</font></blockquote><div class=""><div class=""><br class=""></div><div class="">It does not block the current queue, though. The current queue keeps going on with whatever it was doing before. When the ‘await'ed call finished, it is scheduled back on a queue to continue running.</div><div class=""><br class=""></div><div class="">-BJ</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 25, 2017, at 4:39 PM, Florent Vilmart via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class="">
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class=""><br class="">
On 25 août 2017 18:31 -0400, Jonathan Hull <<a href="mailto:jhull@gbis.com" class="">jhull@gbis.com</a>>, wrote:<br class="">
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;" class="">But then it would need to be called with await under the current proposal, meaning that either:
<div class=""><br class="">
<div class="">• b would await the calculation of a</div>
<div class=""><br class=""></div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>beginAsync {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>let a = await longCalculationA()</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>let b = await longCalculationB() //This only calculates when a is finished</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>} </div>
</div>
</blockquote>
<div class="">why would it? the async modifier could very well be used to transform a sync function into an async one, dispatching on a defined queue returning the 'Future' and resolving once the function completes.</div>
<div class=""><br class=""></div>
<div class="">I'm not sure why it's understood as all or nothing, AFAICU, await is just a way to block the current queue and wait for a result from an async function.</div>
<div class="">beginAsync is a way to make sure we don't block the current execution queue.</div>
<div class=""><br class=""></div>
<div class="">something like</div>
<div class=""><br class=""></div>
<div class="">let a = await async longSynchrounousCall() would block the current execution queue (because await) but the execution of the longSynchronousCall() would be on a separate queue / block.</div>
<div class=""><br class=""></div>
<div class="">The question lays more in where the calls are executed, either they are dispatched after, current block is suspended, runloop continues, and then the block is re-entered upon returning. that would lead to some weird executions flows.</div>
<div class=""><br class=""></div>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;" class="">
<div class="">
<div class=""><br class=""></div>
<div class="">• or b would be executed while a is awaiting, but a and b would be in different scopes</div>
<div class=""><br class=""></div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>beginAsync{</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>let a = await longCalculationA()</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>beginAsync{</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>let b = await longCalculationB() //We can’t see ‘a’ anymore</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>//We can’t see ‘a’ or ‘b’ here to use them</div>
<div class=""><br class=""></div>
<div class="">We could, also implement some sort of future, and then <i class="">re-write</i> our functions to take advantage of it, but this misses out on numerous compiler optimizations and requires our functions to be written with futures in mind. In my example, the functions can just be written as async, and they don’t care whether they are called with async or await.</div>
<div class=""><br class=""></div>
<div class="">Thanks,</div>
<div class="">Jon</div>
<div class=""><br class="">
<div class="">
<blockquote type="cite" class="" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div class="">On Aug 25, 2017, at 3:13 PM, Florent Vilmart <<a href="mailto:florent@flovilmart.com" class="">florent@flovilmart.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">be doesn't wait if it's defined as
<div class=""><br class=""></div>
<div class="">func <span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class="">longCalculationB() async -> SomeType </span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class=""><br class=""></span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class="">which would be helpful if it's a long calculation, </span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class=""><br class=""></span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class="">in the case it's</span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class=""><br class=""></span></div>
<div class="">func <span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class="">longCalculationB() -> SomeType </span><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class=""><br class=""></span></div>
<div class=""><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue';" class=""><br class=""></span></div>
<div class=""><font color="#333333" face="Helvetica Neue" class="">That would probably be valid to put the async keyword front even though I would not be a big fan of that as you'd be executing on an indefinite queue a calculation that may not be thread safe.</font></div>
<div class=""><font color="#333333" face="Helvetica Neue" class=""><br class=""></font></div>
<div class=""><font color="#333333" face="Helvetica Neue" class="">async would be in that case a wrapper around dispatch_async + semaphore</font></div>
<div class=""><br class=""></div>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class=""><br class="">
On 25 août 2017 18:08 -0400, Jonathan Hull <<a href="mailto:jhull@gbis.com" class="">jhull@gbis.com</a>>, wrote:<br class="">
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #3498db;" class="">Why wouldn’t b wait for a in this example? If it is using futures, those aren’t available in the current proposal.
<div class=""><br class="">
<div class="">
<blockquote type="cite" class="" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #d35400;">
<div class="">On Aug 25, 2017, at 3:02 PM, Florent Vilmart <<a href="mailto:florent@flovilmart.com" class="">florent@flovilmart.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">Probably with:
<div class=""><br class=""></div>
<div class="">
<div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(51, 51, 51);" class="">let a = longCalculationA() </div>
<div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(51, 51, 51);" class="">let b = longCalculationB() //b doesn’t wait for a to complete before starting</div>
<div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(51, 51, 51);" class="">let c = longCalculationC() //c doesn’t wait for a or b</div>
<div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(51, 51, 51);" class="">let (aResult, bResult, cResult) = await Future.collect(a, b, c) //waits until a, b, and c are all available</div>
</div>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class=""><br class="">
On 25 août 2017 17:48 -0400, wrote:<br class="">
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #34495e;" class=""><br class="">
<div class="">let a = async longCalculationA()<br class="">
let b = async longCalculationB() //b doesn’t wait for a to complete before starting<br class="">
let c = async longCalculationC() //c doesn’t wait for a or b<br class="">
let result = await combineCalculations(a: a, b: b, c: c) //waits until a, b, and c are all available</div>
</blockquote>
<div class=""></div>
</div>
</div>
</div>
</blockquote>
</div>
<br class=""></div>
</blockquote>
<div class=""></div>
</div>
</div>
</div>
</blockquote>
</div>
<br class=""></div>
</div>
</blockquote>
<div class=""></div>
</div>
</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></div></body></html>