Skip to content

Instantly share code, notes, and snippets.

@domjtalbot
Created May 9, 2019 17:37
Show Gist options
  • Save domjtalbot/ea0e18a49f711cadbb2e40675e20699c to your computer and use it in GitHub Desktop.
Save domjtalbot/ea0e18a49f711cadbb2e40675e20699c to your computer and use it in GitHub Desktop.
Create a pending PromiseKit Promise and delay execution
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