Created
April 13, 2017 05:30
-
-
Save randymarsh77/e2945e876d614261c0f58cdf45836539 to your computer and use it in GitHub Desktop.
A trivial, useless example of async/await using Async that does something and returns something.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Q1 = DispatchQueue(label: "DemoQueue1") | |
let Q2 = DispatchQueue(label: "DemoQueue2") | |
func demo() -> Task<Int> { | |
return async { (task: Task<Int>) in | |
var calculation = 0 | |
Q2.async { | |
print("Doing some expensive calculation of universal truths...") | |
calculation = 42 | |
print("Figured it all out, lets wake up.") | |
Async.Wake(task) | |
} | |
print("Suspend until some work is done...") | |
Async.Suspend() | |
return calculation | |
} | |
} | |
Q1.async { | |
print("The magic number is: \(await (demo()))") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment