Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when(resolved:) tuple overloads #1092

Open
wants to merge 3 commits into
base: v7
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Documents/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pod "PromiseKit/Foundation", "~> 7.0.0-rc1"
pod "PromiseKit/MapKit", "~> 7.0.0-rc1"
```

Considering 7.0 is still a release candidate, you may prefer to use [v6](https://github.com/mxcl/PromiseKit/blob/v6/README.md).

## Carthage

We will support [Carthage] if you can PR an automated solution for generating
Expand Down
35 changes: 24 additions & 11 deletions Sources/PromiseKit/Cancellation/CancellableThenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,6 @@ public extension CancellableThenable {
}

public extension CancellableThenable {
/**
- Returns: The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
*/
var error: Error? {
return thenable.error
}

/**
- Returns: `true` if the cancellable promise has not yet resolved.
*/
Expand All @@ -324,10 +317,10 @@ public extension CancellableThenable {
}

/**
- Returns: `true` if the cancellable promise was rejected.
*/
var isRejected: Bool {
return thenable.isRejected
- Returns: The result with which this cancellable promise was resolved or `nil` if this cancellable promise is pending.
*/
var result: U.R? {
return thenable.result
}

/**
Expand All @@ -338,6 +331,22 @@ public extension CancellableThenable {
}
}

public extension CancellableThenable where U.R == Result<U.T, Error> {
/**
- Returns: The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
*/
var error: Error? {
return thenable.error
}

/**
- Returns: `true` if the cancellable promise was rejected.
*/
var isRejected: Bool {
return thenable.isRejected
}
}

public extension CancellableThenable where U.T: Sequence {
/**
`CancellablePromise<[U.T]>` => `U.T` -> `V` => `CancellablePromise<[V]>`
Expand Down Expand Up @@ -518,3 +527,7 @@ public extension CancellableThenable where U.T: Sequence, U.T.Iterator.Element:
return map(on: on) { $0.sorted() }
}
}

func asThenables<CT: CancellableThenable>(_ cancellableThenables: [CT]) -> [CT.U] {
cancellableThenables.map { $0.thenable }
}
5 changes: 2 additions & 3 deletions Sources/PromiseKit/Guarantee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public final class Guarantee<T>: Thenable {
}

/// - See: `Thenable.result`
public var result: Result<T, Error>? {
public var result: T? {
switch box.inspect() {
case .pending:
return nil
case .resolved(let value):
return .success(value)
return value
}
}

Expand Down Expand Up @@ -163,7 +163,6 @@ public extension Guarantee {
any part of your chain may use. Like the main thread for example.
*/
func wait() -> T {

if Thread.isMainThread {
conf.logHandler(.waitOnMainThread)
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/PromiseKit/Result+Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension Result {

var error: Failure? {
switch self {
case let .failure(error):
return error
case .success:
return nil
}
}
}
55 changes: 29 additions & 26 deletions Sources/PromiseKit/Thenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import Dispatch
public protocol Thenable: AnyObject {
/// The type of the wrapped value
associatedtype T
/// The type of the result
associatedtype R

/// `pipe` is immediately executed when this `Thenable` is resolved
func pipe(to: @escaping(Result<T, Error>) -> Void)

/// The resolved result or nil if pending.
var result: Result<T, Error>? { get }
var result: R? { get }

/// The resolved result's value or nil if pending.
var value: T? { get }
}

public extension Thenable {

/**
The provided closure executes when this promise is fulfilled.

Expand Down Expand Up @@ -218,17 +224,10 @@ public extension Thenable {

public extension Thenable {
/**
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
- Returns: `true` if the promise was fulfilled.
*/
var error: Error? {
switch result {
case .none:
return nil
case .some(.success):
return nil
case .some(.failure(let error)):
return error
}
var isFulfilled: Bool {
return value != nil
}

/**
Expand All @@ -244,33 +243,37 @@ public extension Thenable {
var isResolved: Bool {
return !isPending
}
}

public extension Thenable where R == T {
/**
- Returns: `true` if the promise was fulfilled.
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
*/
var isFulfilled: Bool {
return value != nil
var value: T? {
return result
}
}

public extension Thenable where R == Result<Self.T, Error> {
/**
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
*/
var value: T? {
return try? result?.get()
}

/**
- Returns: `true` if the promise was rejected.
- Returns: `true` if this promise was rejected.
*/
var isRejected: Bool {
return error != nil
}

/**
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
*/
var value: T? {
switch result {
case .none:
return nil
case .some(.success(let value)):
return value
case .some(.failure):
return nil
}
var error: Error? {
return result?.error
}
}

Expand Down