Skip to content

Instantly share code, notes, and snippets.

@dimitribouniol
Created February 13, 2022 23:47
Show Gist options
  • Save dimitribouniol/be24e9974be5c01b82c4e1627272aa77 to your computer and use it in GitHub Desktop.
Save dimitribouniol/be24e9974be5c01b82c4e1627272aa77 to your computer and use it in GitHub Desktop.
Async DispactQueue.async
extension DispatchQueue {
func async<T>(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], execute work: @escaping () -> T) async -> T {
await withCheckedContinuation { continuation in
self.async(group: group, qos: qos, flags: flags) {
let result = work()
continuation.resume(returning: result)
}
}
}
}
Task {
print("calling sync function")
await DispatchQueue.global().async {
print("starting async function")
sleep(5)
print("stopping async function")
}
print("finished with sync function")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment