Skip to content

Instantly share code, notes, and snippets.

@standinga
Created August 30, 2020 01:42
Show Gist options
  • Save standinga/5fdc2cbac3e50541da691004033232ca to your computer and use it in GitHub Desktop.
Save standinga/5fdc2cbac3e50541da691004033232ca to your computer and use it in GitHub Desktop.
func combineAsyncCalls(completionHandler: @escaping (String)->()) {
var text = ""
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.global().async {
fetchData(0, delay: 0.4) {
text += $0
semaphore.signal()
}
fetchData(1, delay: 0.2) {
text += $0
semaphore.signal()
}
semaphore.wait()
semaphore.wait()
completionHandler(text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment