diff --git a/Sources/AwaitKit/AwaitKit.swift b/Sources/AwaitKit/AwaitKit.swift index 4902dad..cfc5c84 100644 --- a/Sources/AwaitKit/AwaitKit.swift +++ b/Sources/AwaitKit/AwaitKit.swift @@ -43,6 +43,15 @@ public func async(_ body: @escaping () throws -> T) -> Promise { return Queue.async.async(.promise, execute: body) } +/** + Yields the execution to the given closure and returns a new guarantee + - parameter body: The closure that is executed on a concurrent queue. + - returns: A new guarantee that is resolved when the provided closure returned. + */ +public func async(_ body: @escaping () -> T) -> Guarantee { + return Queue.async.async(.promise, execute: body) +} + /** Yields the execution to the given closure which returns nothing. - parameter body: The closure that is executed on a concurrent queue. diff --git a/Tests/AwaitKitTests/AwaitKitAsyncTests.swift b/Tests/AwaitKitTests/AwaitKitAsyncTests.swift index 5fb389b..91d9f18 100644 --- a/Tests/AwaitKitTests/AwaitKitAsyncTests.swift +++ b/Tests/AwaitKitTests/AwaitKitAsyncTests.swift @@ -53,6 +53,26 @@ class AwaitKitAsyncTests: XCTestCase { } } + func testSimpleDelayedValidGuaranteeAsyncBlock() { + let expect = expectation(description: "Async should return value") + + let guarantee: Guarantee = async { + Thread.sleep(forTimeInterval: 0.2) + + return "AwaitedPromiseKit" + } + + _ = guarantee.done { value in + expect.fulfill() + } + + waitForExpectations(timeout: 0.5) { error in + if error == nil { + XCTAssertEqual(guarantee.value, "AwaitedPromiseKit") + } + } + } + func testSimpleFailedAsyncBlock() { let expect = expectation(description: "Async should not return value")