How to sync an async function in Swift

Issue #547

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func sync<T>(_ work: (@escaping ([T]) -> Void) -> Void) -> [T] {
let semaphore = DispatchSemaphore(value: 1)
var results = [T]()
work { values in
results = values
semaphore.signal()
}

return results
}

sync({ completion in
service.load(completion)
})

Comments