Skip to content

Commit 4acf52e

Browse files
committed
add when(fulfilled:) and when(resolved:) tuple arity functions for thenables and cancellable thenables
add an associated type R representing the result type in Thenable which improves when(resolved:) return type
1 parent dc04daa commit 4acf52e

File tree

11 files changed

+1404
-510
lines changed

11 files changed

+1404
-510
lines changed

Sources/PromiseKit/Cancellation/CancellableThenable.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -296,48 +296,55 @@ public extension CancellableThenable {
296296

297297
public extension CancellableThenable {
298298
/**
299-
- Returns: The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
300-
*/
301-
var error: Error? {
302-
return thenable.error
303-
}
304-
305-
/**
306-
- Returns: `true` if the cancellable promise has not yet resolved.
299+
- `true` if the cancellable promise has not yet resolved.
307300
*/
308301
var isPending: Bool {
309302
return thenable.isPending
310303
}
311304

312305
/**
313-
- Returns: `true` if the cancellable promise has resolved.
306+
- `true` if the cancellable promise has resolved.
314307
*/
315308
var isResolved: Bool {
316309
return thenable.isResolved
317310
}
318311

319312
/**
320-
- Returns: `true` if the cancellable promise was fulfilled.
313+
- `true` if the cancellable promise was fulfilled.
321314
*/
322315
var isFulfilled: Bool {
323316
return thenable.isFulfilled
324317
}
325318

326-
/**
327-
- Returns: `true` if the cancellable promise was rejected.
328-
*/
329-
var isRejected: Bool {
330-
return thenable.isRejected
319+
/// The result with which this cancellable promise was resolved or `nil` if this cancellable promise is pending.
320+
var result: U.R? {
321+
return thenable.result
331322
}
332323

333324
/**
334-
- Returns: The value with which this cancellable promise was fulfilled or `nil` if this cancellable promise is pending or rejected.
325+
- The value with which this cancellable promise was fulfilled or `nil` if this cancellable promise is pending or rejected.
335326
*/
336327
var value: U.T? {
337328
return thenable.value
338329
}
339330
}
340331

332+
public extension CancellableThenable where U.R == Result<U.T, Error> {
333+
/**
334+
- The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
335+
*/
336+
var error: Error? {
337+
return thenable.error
338+
}
339+
340+
/**
341+
- `true` if the cancellable promise was rejected.
342+
*/
343+
var isRejected: Bool {
344+
return thenable.isRejected
345+
}
346+
}
347+
341348
public extension CancellableThenable where U.T: Sequence {
342349
/**
343350
`CancellablePromise<[U.T]>` => `U.T` -> `V` => `CancellablePromise<[V]>`
@@ -479,7 +486,7 @@ public extension CancellableThenable where U.T: Sequence {
479486
}
480487

481488
public extension CancellableThenable where U.T: Collection {
482-
/// - Returns: a cancellable promise fulfilled with the first value of this `Collection` or, if empty, a promise rejected with PMKError.emptySequence.
489+
/// - a cancellable promise fulfilled with the first value of this `Collection` or, if empty, a promise rejected with PMKError.emptySequence.
483490
var firstValue: CancellablePromise<U.T.Iterator.Element> {
484491
return map(on: nil) { aa in
485492
if let a1 = aa.first {
@@ -499,7 +506,7 @@ public extension CancellableThenable where U.T: Collection {
499506
}
500507
}
501508

502-
/// - Returns: a cancellable promise fulfilled with the last value of this `Collection` or, if empty, a promise rejected with PMKError.emptySequence.
509+
/// - a cancellable promise fulfilled with the last value of this `Collection` or, if empty, a promise rejected with PMKError.emptySequence.
503510
var lastValue: CancellablePromise<U.T.Iterator.Element> {
504511
return map(on: nil) { aa in
505512
if aa.isEmpty {
@@ -518,3 +525,7 @@ public extension CancellableThenable where U.T: Sequence, U.T.Iterator.Element:
518525
return map(on: on) { $0.sorted() }
519526
}
520527
}
528+
529+
func asThenables<CT: CancellableThenable>(_ cancellableThenables: [CT]) -> [CT.U] {
530+
cancellableThenables.map { $0.thenable }
531+
}

Sources/PromiseKit/Guarantee.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public final class Guarantee<T>: Thenable {
5151
}
5252

5353
/// - See: `Thenable.result`
54-
public var result: Result<T, Error>? {
54+
public var result: T? {
5555
switch box.inspect() {
5656
case .pending:
5757
return nil
5858
case .resolved(let value):
59-
return .success(value)
59+
return value
6060
}
6161
}
6262

@@ -163,7 +163,6 @@ public extension Guarantee {
163163
any part of your chain may use. Like the main thread for example.
164164
*/
165165
func wait() -> T {
166-
167166
if Thread.isMainThread {
168167
conf.logHandler(.waitOnMainThread)
169168
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
3+
extension Result {
4+
5+
var error: Failure? {
6+
switch self {
7+
case let .failure(error):
8+
return error
9+
case .success:
10+
return nil
11+
}
12+
}
13+
}

Sources/PromiseKit/Thenable.swift

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import Dispatch
44
public protocol Thenable: AnyObject {
55
/// The type of the wrapped value
66
associatedtype T
7+
/// The type of the result
8+
associatedtype R
79

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

1113
/// The resolved result or nil if pending.
12-
var result: Result<T, Error>? { get }
14+
var result: R? { get }
15+
16+
/// The resolved result's value or nil if pending.
17+
var value: T? { get }
1318
}
1419

1520
public extension Thenable {
21+
1622
/**
1723
The provided closure executes when this promise is fulfilled.
1824

@@ -218,59 +224,56 @@ public extension Thenable {
218224

219225
public extension Thenable {
220226
/**
221-
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
227+
- Returns: `true` if this thenable was fulfilled.
222228
*/
223-
var error: Error? {
224-
switch result {
225-
case .none:
226-
return nil
227-
case .some(.success):
228-
return nil
229-
case .some(.failure(let error)):
230-
return error
231-
}
229+
var isFulfilled: Bool {
230+
return value != nil
232231
}
233232

234233
/**
235-
- Returns: `true` if the promise has not yet resolved.
234+
- Returns: `true` if this thenable has not yet resolved.
236235
*/
237236
var isPending: Bool {
238237
return result == nil
239238
}
240239

241240
/**
242-
- Returns: `true` if the promise has resolved.
241+
- Returns: `true` if this thenable has resolved.
243242
*/
244243
var isResolved: Bool {
245244
return !isPending
246245
}
246+
}
247247

248+
public extension Thenable where R == T {
248249
/**
249-
- Returns: `true` if the promise was fulfilled.
250+
- Returns: The value with which this thenable was fulfilled or `nil` if this thenable is pending.
250251
*/
251-
var isFulfilled: Bool {
252-
return value != nil
252+
var value: T? {
253+
return result
253254
}
255+
}
254256

257+
public extension Thenable where R == Result<Self.T, Error> {
255258
/**
256-
- Returns: `true` if the promise was rejected.
259+
- Returns: The value with which this thenable was fulfilled or `nil` if this thenable is pending or rejected.
260+
*/
261+
var value: T? {
262+
return try? result?.get()
263+
}
264+
265+
/**
266+
- Returns: `true` if this thenable was rejected.
257267
*/
258268
var isRejected: Bool {
259269
return error != nil
260270
}
261-
271+
262272
/**
263-
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
273+
- Returns: The error with which this thenable was rejected; `nil` if this thenable is not rejected or pending.
264274
*/
265-
var value: T? {
266-
switch result {
267-
case .none:
268-
return nil
269-
case .some(.success(let value)):
270-
return value
271-
case .some(.failure):
272-
return nil
273-
}
275+
var error: Error? {
276+
return result?.error
274277
}
275278
}
276279

0 commit comments

Comments
 (0)