diff --git a/Sources/Hydra/Promise+Await.swift b/Sources/Hydra/Promise+Await.swift index 6801389..9fd19dd 100644 --- a/Sources/Hydra/Promise+Await.swift +++ b/Sources/Hydra/Promise+Await.swift @@ -66,8 +66,9 @@ public prefix func ..! (_ promise: Promise) -> T? { /// - Returns: fulfilled value of the promise /// - Throws: throws an exception if promise fails due to an error @discardableResult +@available(*, deprecated, renamed: "Hydra.await") public func await(in context: Context? = nil, _ promise: Promise) throws -> T { - return try (context ?? awaitContext).await(promise) + return try Hydra.await(in: context, promise) } /// Awaits that the given body is resolved. This is a shortcut which simply create a Promise; as for a Promise you need to @@ -79,9 +80,39 @@ public func await(in context: Context? = nil, _ promise: Promise) throws - /// - Returns: the value of the promise /// - Throws: an exception if operation fails @discardableResult +@available(*, deprecated, renamed: "Hydra.await") public func await(in context: Context = .background, _ body: @escaping ((_ fulfill: @escaping (T) -> (), _ reject: @escaping (Error) -> (), _ operation: PromiseStatus) throws -> ())) throws -> T { - let promise = Promise(in: context, body) - return try await(in: context, promise) + return try Hydra.await(in: context, body) +} + +public enum Hydra { + + /// Awaits that the given promise fulfilled with its value or throws an error if the promise fails + /// + /// - Parameters: + /// - context: context in which you want to execute the operation. If not specified default concurrent `awaitContext` is used instead. + /// - promise: target promise + /// - Returns: fulfilled value of the promise + /// - Throws: throws an exception if promise fails due to an error + @discardableResult + public static func await(in context: Context? = nil, _ promise: Promise) throws -> T { + return try (context ?? awaitContext).await(promise) + } + + /// Awaits that the given body is resolved. This is a shortcut which simply create a Promise; as for a Promise you need to + /// call `resolve` or `reject` in order to complete it. + /// + /// - Parameters: + /// - context: context in which the body is executed (if not specified `background` is used) + /// - body: closure to execute + /// - Returns: the value of the promise + /// - Throws: an exception if operation fails + @discardableResult + public static func await(in context: Context = .background, _ body: @escaping ((_ fulfill: @escaping (T) -> (), _ reject: @escaping (Error) -> (), _ operation: PromiseStatus) throws -> ())) throws -> T { + let promise = Promise(in: context, body) + return try Hydra.await(in: context, promise) + } + } diff --git a/Tests/HydraTests/HydraTests.swift b/Tests/HydraTests/HydraTests.swift index d92f6aa..0de5930 100644 --- a/Tests/HydraTests/HydraTests.swift +++ b/Tests/HydraTests/HydraTests.swift @@ -578,7 +578,7 @@ class HydraTestThen: XCTestCase { do { let startValue = 5 // let result1 = try ..self.intPromise(startValue) - let result1 = try await(self.intPromise(startValue)) + let result1 = try Hydra.await(self.intPromise(startValue)) let result2 = try ..self.intPromiseDelay(result1 * 2, delay: 0.5) let result3 = try ..self.intPromiseDelay(result2 * 2, delay: 0.5) if result3 == startValue * 4 {