@@ -466,15 +466,15 @@ public extension Future {
466
466
/// Note that the provided callback is called regardless of whether this future is fulfilled or rejected.
467
467
/// The returned `Future<T>` is fulfilled **only** if this and the provided future both are fullfilled.
468
468
///
469
- /// In essence , the returned future will forward the result of this future, if the provided future
469
+ /// In short , the returned future will forward the result of this future, if the provided future
470
470
/// is fulfilled.
471
471
///
472
472
/// - Parameters:
473
473
/// - queue: Dispatch queue to observe on.
474
474
/// - initialResult: An initial result to begin the reduction.
475
475
/// - callback: A callback that returns a `Future<Void>` to be deferred.
476
476
/// - Returns: A future that will receive the eventual value.
477
- func `defer` ( on queue: DispatchQueue = . futures, callback: @escaping ( ) -> Future < Void > ) -> Future < T > {
477
+ func always ( on queue: DispatchQueue = . futures, callback: @escaping ( ) -> Future < Void > ) -> Future < T > {
478
478
let promise = Promise < T > ( )
479
479
480
480
whenResolved ( on: queue) { result1 in
@@ -497,6 +497,29 @@ public extension Future {
497
497
return promise. future
498
498
}
499
499
500
+ /// Returns a new `Future<T>` that will resolve with the result of this `Future` **after** the provided callback
501
+ /// runs.
502
+ ///
503
+ /// Note that the provided callback is called regardless of whether this future is fulfilled or rejected.
504
+ ///
505
+ /// This method is useful for times you want to execute code at the end of a chain of operations, regarless
506
+ /// of whether successful or not.
507
+ ///
508
+ /// - Parameters:
509
+ /// - queue: Dispatch queue to observe on.
510
+ /// - callback: Callback to run.
511
+ /// - Returns: A future that will receive the eventual value.
512
+ func `defer`( on queue: DispatchQueue = . futures, callback: @escaping ( ) -> Void ) -> Future < T > {
513
+ let promise = Promise < T > ( )
514
+
515
+ whenResolved ( on: queue) { result in
516
+ callback ( )
517
+ promise. resolve ( result)
518
+ }
519
+
520
+ return promise. future
521
+ }
522
+
500
523
enum PollError : Error {
501
524
case retryCountExceeded
502
525
}
@@ -507,14 +530,14 @@ public extension Future {
507
530
/// is acceptable.
508
531
///
509
532
/// - Parameters:
510
- /// - dispatchQueue : Dispatch queue to poll on,
533
+ /// - queue : Dispatch queue to poll on,
511
534
/// - future: Future used as the polling function.
512
535
/// - interval: Time to wait between invoking subsequent futures.
513
536
/// - retryCount: maxRetryCount Maximum number of times to invoke the supplied future
514
537
/// - finished: Handler to invoke, resolving whether the result of the given promise is acceptable
515
538
/// - Returns: A future that will receive the eventual value.
516
539
static func poll< T> (
517
- on dispatchQueue : DispatchQueue = . futures,
540
+ on queue : DispatchQueue = . futures,
518
541
future: @escaping @autoclosure ( ) -> Future < T > ,
519
542
interval: TimeInterval ,
520
543
maxRetryCount: Int ,
@@ -534,7 +557,7 @@ public extension Future {
534
557
if finished ( value) {
535
558
promise. fulfill ( value)
536
559
} else if numberOfRetries < maxRetryCount {
537
- dispatchQueue . asyncAfter ( deadline: . now( ) + ( numberOfRetries < 1 ? 0 : interval) ) {
560
+ queue . asyncAfter ( deadline: . now( ) + ( numberOfRetries < 1 ? 0 : interval) ) {
538
561
poll ( )
539
562
}
540
563
} else {
@@ -551,6 +574,28 @@ public extension Future {
551
574
552
575
return promise. future
553
576
}
577
+
578
+ /// Returns a new Future<Void>, effectively discarding the result of the caller.
579
+ ///
580
+ /// This method is useful when the value of a future is of no consequence.
581
+ ///
582
+ /// - Parameter queue: Dispatch queue to discard on.
583
+ /// - Returns: A future that will receive the eventual value.
584
+ @discardableResult
585
+ func discard( on queue: DispatchQueue = . futures) -> Future < Void > {
586
+
587
+ let promise = Promise < Void > ( )
588
+
589
+ whenFulfilled ( on: queue) { _ in
590
+ promise. fulfill ( )
591
+ }
592
+
593
+ whenRejected { error in
594
+ promise. reject ( error)
595
+ }
596
+
597
+ return promise. future
598
+ }
554
599
}
555
600
556
601
public extension Future {
0 commit comments