Created
July 28, 2016 19:05
-
-
Save sammoore/8215e409783e8ae3e7c80254bcb4f921 to your computer and use it in GitHub Desktop.
ReverseAsync.swift
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
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