Created
February 4, 2017 11:19
-
-
Save malcommac/c4afa9cee1766183d3718add59f3c704 to your computer and use it in GitHub Desktop.
Hydra_Internals_14.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
public extension Context { | |
internal func await<T>(_ promise: Promise<T>) throws -> T { | |
guard self.queue != DispatchQueue.main else { | |
throw PromiseError.invalidContext | |
} | |
var result: T? | |
var error: Error? | |
let semaphore = DispatchSemaphore(value: 0) | |
promise.then(in: self) { value -> Void in | |
result = value | |
semaphore.signal() | |
}.catch(in: self) { err in | |
error = err | |
semaphore.signal() | |
} | |
_ = semaphore.wait(timeout: DispatchTime(uptimeNanoseconds: UINT64_MAX)) | |
guard let promiseValue = result else { | |
throw error! | |
} | |
return promiseValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment