Skip to content
Merged
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
7 changes: 7 additions & 0 deletions Sources/AnyPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ typedef void (^PMKResolver)(id __nullable) NS_REFINED_FOR_SWIFT;
*/
- (AnyPromise * __nonnull(^ __nonnull)(dispatch_queue_t __nonnull, dispatch_block_t __nonnull))ensureOn NS_REFINED_FOR_SWIFT;

/**
Wait until the promise is resolved.

@return Value if fulfilled or error if rejected.
*/
- (id __nullable)wait NS_REFINED_FOR_SWIFT;

/**
Create a new promise with an associated resolver.

Expand Down
4 changes: 4 additions & 0 deletions Sources/AnyPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ - (id)__d {
};
}

- (id)wait {
return [d __wait];
}

- (BOOL)pending {
return [[d valueForKey:@"__pending"] boolValue];
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/AnyPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ import Foundation
}))
}

@objc public func __wait() -> Any? {
if Thread.isMainThread {
conf.logHandler(.waitOnMainThread)
}

var result = __value

if result == nil {
let group = DispatchGroup()
group.enter()
self.__pipe { obj in
result = obj
group.leave()
}
group.wait()
}

return result
}

/// Internal, do not use! Some behaviors undefined.
@objc public func __pipe(_ to: @escaping (Any?) -> Void) {
let to = { (obj: Any?) -> Void in
Expand Down
16 changes: 16 additions & 0 deletions Tests/CoreObjC/AnyPromiseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,22 @@ - (void)test_60_catch_on_specific_queue {
[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)test_61_wait_for_value {
id o = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
resolve(@1);
}].wait;

XCTAssertEqualObjects(o, @1);
}

- (void)test_62_wait_for_error {
NSError* err = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
resolve([NSError errorWithDomain:@"a" code:123 userInfo:nil]);
}].wait;

XCTAssertEqual(err.code, 123);
}

- (void)test_properties {
XCTAssertEqualObjects([AnyPromise promiseWithValue:@1].value, @1);
XCTAssertEqualObjects([[AnyPromise promiseWithValue:dummyWithCode(2)].value localizedDescription], @"2");
Expand Down