WWDC21 introduces Swift 5.5, with async/await. Following the Explore structured concurrency in Swift and Meet async/await in Swift WWDC21 sessions, I'm trying to use the async let function.
Here's my Playground code:
func doIt() async -> String {
let t = TimeInterval.random(in: 0.25 ... 2.0)
Thread.sleep(forTimeInterval: t)
return String("\(Double.random(in: 0...1000))")
}
async {
async let a = doIt()
async let b = doIt()
async let c = doIt()
async let d = doIt()
let results = await [a, b, c, d]
for result in results {
print(" \(result)")
}
}
However, for each of the "async let" lines, I get this error:
error: AsyncLetSwift55WWDC21.playground:12:15: error: expression is 'async' but is not marked with 'await'
async let a = doIt()
^
await
Paul Hudson's blog showed this example:
The Exploring structured currency video has this example at about the 8:10 mark:
EDIT: This does seem to be a Playground-specific issue. Per the suggestion on the same issue in Apple Developer Forums, running the same code (ok, I did add sleep(10)
to the end of the source file after the async block for macOS, so the app wouldn't terminate before the async calls completed) as a macOS command-line project gives no errors and produces the proper output.
Is this a bug, or am I just not understanding something?