Skip to content

Instantly share code, notes, and snippets.

@randymarsh77
Created April 13, 2017 05:30
Show Gist options
  • Save randymarsh77/e2945e876d614261c0f58cdf45836539 to your computer and use it in GitHub Desktop.
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.
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