withContinuation(of:throwing:_:)
Invokes the passed in closure with a non-copyable continuation for the current task.
Declaration
nonisolated(nonsending) func withContinuation<Success, Failure>(of: Success.Type = Success.self, throwing: Failure.Type, _ body: (consuming Continuation<Success, Failure>) -> Void) async throws(Failure) -> sending Success where Failure : Error, Success : ~CopyableParameters
- of:
The
Successtype returned by the continuation - throwing:
The
Failuretype that may be thrown - body:
A closure that takes a
Continuationparameter
Return Value
The value the continuation is resumed with
Discussion
The body of the closure executes synchronously on the calling task, and once it returns the calling task is suspended. It is possible to immediately resume the task, or escape the continuation in order to complete it afterwards, which will then resume the suspended task.
You must invoke the continuation’s resume method exactly once. The continuation is a noncopyable type, and therefore multiple resume calls are prevented at compile time (as resuming the continuation consumes it). However, if the continuation is dropped without being resumed, the program traps.