Async/let producing error in playground? Xcode 13.0 beta

I'm seeing an error trying to test out async let. It seems like this should work, based on the async/let and structured concurrency session videos.

Here's the 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)")
    }
}

But, I get this error for every "async let" statement:

error: AsyncLetSwift55WWDC21.playground:12:15: error: expression is 'async' but is not marked with 'await'
    async let a = doIt()
              ^
              await 

Am I missing something key, or is this a bug?

I'm running this in the Xcode 13.0 beta, in a Playground.

Thanks!!

Are you running Xcode 13 on macOS 12 beta? Or macOS 11?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

If you put the same code in a command-line tool project (created from the macOS > Command Line Tool template) do you get the error there?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

So maybe playground-specific?

Sounds like it.

When you did your playground testing, were you using an iOS playground? If so, try a macOS one.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

i build a iOS project on Xcode 13.0 beta, and this code works on simulator

@eskimo Do we have to file a bug for playgrounds? I am still on macOS BigSur, so can't run it on a command line project as it would require macOS 12 (Beta). I was preparing some tutorials to demonstrate async let so thought playgrounds was ideal as opposed to an iOS project

When running this code from command line it works fine.System Version: macOS 12.0 (21A5294g)

Tested Playgrounds both iOS and Mac platform. Both failed but the code is fine. It is just a Playground issue.

Could someone make sure this bug was filed with the XCode Playgrounds team.

I am still on macOS BigSur, so can't run it on a command line project

If you’re on macOS 11 then you can’t run async/await code natively because macOS 11 does not include the necessary runtime bits. The solution is to target iOS:

  • You can create an iOS app and run it on an iOS 15 beta device

  • Or on an iOS 15 beta simulator.

  • Or, if you want to work in a playground, create an iOS playground which will run on the iOS 15 beta simulator.

With regards that last point, here’s how I tested this today:

  1. On macOS 11.4, I ran Xcode 13.0b4.

  2. I chose File > New > Playground and then selected iOS > Blank.

  3. I changed the playground’s code to this:

    import UIKit
    import PlaygroundSupport
    
    PlaygroundPage.current.needsIndefiniteExecution = true
    
    print("will start task")
    Task.detached {
        print("task will sleep")
        try! await Task.sleep(nanoseconds: 1_000_000_000)
        print("task did sleep")
    }
    print("did start task")
    
  4. I ran the playground. It printed this:

    will start task
    task will sleep
    did start task
    

    and then, one second later, printed this:

    task did sleep
    

Step 4 can take a while the first time around as the playground spins up a simulator instance in the background.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo we know

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)")
    }
}

will not run in a Playground. We need the bug fixed :-)

We need the bug fixed

Which bug specifically? What’s the bug number?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can eskimo open a bug for it?

It’s better if you file your own bug report; that way you can monitor its state.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo

Feedback

I have file a feedback FB9546874

Versions:

  • The issue happens with Xcode Playground (iOS)
  • Xcode: Version 13.0 beta 5 (13A5212g)
  • macOS: 11.5.2 (20G95)

Note:

  • The same code runs fine in a SwiftUI project

Code:

import UIKit

func f1() async -> Int {
    return 10
}

Task {
    async let a = f1()
    async let b = f1()
    
    let c = await a + b
    print("c = \(c)")
}

Error:

error: AsyncLet.playground:8:15: error: expression is 'async' but is not marked with 'await'
    async let a = f1()
              ^
              await 

error: AsyncLet.playground:9:15: error: expression is 'async' but is not marked with 'await'
    async let b = f1()
              ^
              await 

I have file a feedback FB9546874

Thank you!

I had a look at your bug and it seems to have landed in the right place but, as you only filed it yesterday, I have no other info to share right now.

I’m hoping that it will get closed as a dup of an earlier bug that’s been investigated already but we’ll have to see how that pans out. In the meantime, watch out for future Xcode beta seeds and retry your test and post back here with the results.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Async/let producing error in playground? Xcode 13.0 beta
 
 
Q