[Concurrency] How do I write an async function that can suspend with the current toolchain

Now that I got the concurrency toolchain running, I have another question: is it possible with the current toolchain (I'm using 2020-11-17) to write an async function that actually suspends?

I can write a function that is marked async but never actually suspends:

func compute() async -> Int {
  return 42
}

But if I want to do something that's really asynchronous (say, perform some I/O or just sleep for a few seconds), if I understand correctly, I have to do one of two things:

  1. Call a system API that is itself async. These don't exist yet, for obvious reasons. Standard library APIs, such as Task.sleep, are not yet implemented in the current toolchain.

  2. Use Task.withUnsafeContinuation to bridge between the async world and the old, callback-based world. This doesn't work because Task.withUnsafeContinuation and friends aren't implemented yet, either.

Is there another option?

2 Likes

Runtime support is basically nonexistent at this point (though basic support for starting tasks is starting to land). You can write code that should compile, and it should typecheck, but don't expect to be able to execute anything just yet.

1 Like

Alright, thanks for confirming.