I have a callback based API.
func start(_ completion: @escaping () -> Void)
I'd like to write an async/await wrapper on top of that API, deferring the implementation to original callback based API.
func start() async {
let task = Task()
start {
task.fulfill()
}
return await task
}
Obviously, this code doesn't connect - it's not real, there is no method fulfill
on Task
.
Question: is there a way to use unstructured concurrency in Swift so that it would help me achieve this?
You are looking for continuations .
Here is how to use it in your example:
func start() async {
await withCheckedContinuation { continuation in
start(continuation.resume)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.