Skip to content

Commit

Permalink
fix malcommac#86: suppress Xcode 12.5 await function warning. Now it …
Browse files Browse the repository at this point in the history
…should be called inside Hydra.await()
  • Loading branch information
malcommac committed Apr 25, 2021
1 parent 41964c2 commit ea5ff14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
37 changes: 34 additions & 3 deletions Sources/Hydra/Promise+Await.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public prefix func ..!<T> (_ promise: Promise<T>) -> 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<T>(in context: Context? = nil, _ promise: Promise<T>) 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
Expand All @@ -79,9 +80,39 @@ public func await<T>(in context: Context? = nil, _ promise: Promise<T>) throws -
/// - Returns: the value of the promise
/// - Throws: an exception if operation fails
@discardableResult
@available(*, deprecated, renamed: "Hydra.await")
public func await<T>(in context: Context = .background, _ body: @escaping ((_ fulfill: @escaping (T) -> (), _ reject: @escaping (Error) -> (), _ operation: PromiseStatus) throws -> ())) throws -> T {
let promise = Promise<T>(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<T>(in context: Context? = nil, _ promise: Promise<T>) 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<T>(in context: Context = .background, _ body: @escaping ((_ fulfill: @escaping (T) -> (), _ reject: @escaping (Error) -> (), _ operation: PromiseStatus) throws -> ())) throws -> T {
let promise = Promise<T>(in: context, body)
return try Hydra.await(in: context, promise)
}

}


Expand Down
2 changes: 1 addition & 1 deletion Tests/HydraTests/HydraTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ea5ff14

Please sign in to comment.