Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created February 4, 2017 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcommac/c4afa9cee1766183d3718add59f3c704 to your computer and use it in GitHub Desktop.
Save malcommac/c4afa9cee1766183d3718add59f3c704 to your computer and use it in GitHub Desktop.
Hydra_Internals_14.swift
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