<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=""><div><blockquote type="cite" class=""><div class="">On Sep 17, 2017, at 3:52 AM, Trevör ANNE DENISE 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="">Hello everyone,<br class=""><br class="">I have a few questions about async await in Swift.<br class=""><br class="">Say that you have :<br class=""><br class="">func foo() async {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("Hey")<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>await bar()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("How are you ?")<br class="">}<br class=""><br class="">First of all, am I right to say that :<br class="">1) If the bar function wasn't an async function, the thread would be blocked until bar returns, at this point print("How are you ?") would be executed and its only after that that the function calling foo() would get back "control"<br class=""></div></div></blockquote><div><br class=""></div><div>I don't think you can quite call await without marking foo() as async (?).</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">2) Here (with async bar function), if bar() takes some time to execute,</div></div></blockquote><div><br class=""></div><div>Not quite, `await bar()` is afaict syntactic sugar for:</div><div><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div>bar {</div></div><div><div>&nbsp; &nbsp; printf("How are you ?");</div></div><div><div>}</div></div></blockquote><div><br class=""></div><div>Where bar used to take a closure before, the compiler is just making it for you. bar itself will be marked async and will handle its asynchronous nature e.g. using dispatch or something else entirely.</div><div>This has nothing to do with "time".</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""> control is directly given back to the function calling foo() and, when bar() returns, print("How are you") will be executed.<br class=""><br class=""><br class="">Second question about why async/await are needed:<br class="">Since calling await must be done in an async function and since async function can only be called, then if we have :<br class="">func baz() async {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>await foo()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("Something else")<br class="">}<br class=""><br class="">Does this mean that "print("Something else")" will actually be waiting for bar() (and foo()) to complete ?<br class="">If this is right, then, what surprises me a little is that in this specific case, if all functions hadn't been async, then the execution order would have exactly been the same, am I right ?<br class="">So why are async/await needed ? Except for clarity, what do they enable that wouldn't have been possible otherwise ? It's not exactly clear to me sometimes because even things like futures wouldn't seem impossible to build without async await.<br class=""><br class="">About beginAsync, say that we do that on the main thread :<br class="">beginAsync {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>await someAsyncFunction()<br class="">}<br class=""><br class="">Will someAsyncFunction() still be executed on the main thread if it was defined this way ?<br class="">func someAsyncFunction() async {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for element in manyElements {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>doSomethingLong(element)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class="">In this case, when will the main "choose" to execute those instructions ?<br class=""><br class=""><br class="">Thank you !<br class=""><br class="">Trevör<br class=""><br class=""><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=""></body></html>