[Concurrency] Evolving the Concurrency design and proposals

This is exactly the use case of withUnsafeContinuation:

func request() async -> SomeResult {
    Task.withUnsafeContinuation { continuation in
        requestWithClosure { result in
            continuation.resume(returning: result) }
        }
    }
}

The unsafeness is unavoidable without extra language features to support the usage restrictions: continuations must be invoked exactly once and there must be no code executed after the continuation.

With such restrictions, it might be possible to provide a “safe” version that could be used in the simplest use cases, but see the section “The challenge with diagnostics for Unsafe” in Structured Concurrency.

3 Likes