Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/AwaitKit/AwaitKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public func async<T>(_ body: @escaping () throws -> T) -> Promise<T> {
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<T>(_ body: @escaping () -> T) -> Guarantee<T> {
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.
Expand Down
20 changes: 20 additions & 0 deletions Tests/AwaitKitTests/AwaitKitAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ class AwaitKitAsyncTests: XCTestCase {
}
}

func testSimpleDelayedValidGuaranteeAsyncBlock() {
let expect = expectation(description: "Async should return value")

let guarantee: Guarantee<String> = 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")

Expand Down