Created
February 13, 2022 23:47
-
-
Save dimitribouniol/be24e9974be5c01b82c4e1627272aa77 to your computer and use it in GitHub Desktop.
Async DispactQueue.async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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