Created
May 9, 2019 17:37
-
-
Save domjtalbot/ea0e18a49f711cadbb2e40675e20699c to your computer and use it in GitHub Desktop.
Create a pending PromiseKit Promise and delay execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PromiseKit | |
/// Create a Promise pending execution | |
/// | |
/// By default Promises will execute on creation and | |
/// wait to be resolved. In general this behaviour is fine, | |
/// however there may be circumstances where this causes | |
/// unexpected behaviour. | |
/// For example when using nested promises. | |
/// | |
/// `PendingPromise` allows a promise to be defined | |
/// and only executed when told to. | |
/// | |
/// | |
/// - note: | |
/// To execute a pending Promise call the `.run()` method | |
public class PendingPromise<T> { | |
/// Pending promise | |
private let promise: () -> Promise<T> | |
public init(promise: @escaping () -> Promise<T>) { | |
self.promise = promise | |
} | |
/// Execute pending promise | |
/// | |
/// - returns: Pending promise | |
public func run() -> Promise<T> { | |
return promise() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment