Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created July 28, 2016 19:05
Show Gist options
  • Save sammoore/8215e409783e8ae3e7c80254bcb4f921 to your computer and use it in GitHub Desktop.
Save sammoore/8215e409783e8ae3e7c80254bcb4f921 to your computer and use it in GitHub Desktop.
ReverseAsync.swift
func ReverseAsync<ReturnType>() -> (completionHandler: ReturnType -> Void, complete: () -> ReturnType) {
var value: Optional<ReturnType> = .None
let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(0)
return (
completionHandler: { v in
value = .Some(v)
dispatch_semaphore_signal(semaphore)
},
complete: {
func wait() -> ReturnType {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
guard case .Some(let v) = value else { fatalError() }
return v
}
switch value {
case .None: return wait()
case .Some(let value): return value
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment