Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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