Skip to content

Instantly share code, notes, and snippets.

@Enriquecm
Created October 25, 2021 15:31
Show Gist options
  • Save Enriquecm/732ee2b4afbd39637bfa1ebe2ed4344a to your computer and use it in GitHub Desktop.
Save Enriquecm/732ee2b4afbd39637bfa1ebe2ed4344a to your computer and use it in GitHub Desktop.
Simplest way to use promises
import Foundation
class Promise<Value> {
enum State<T> {
case completed(T)
case pending
}
private var state: State<Value> = .pending
private var callbacks: [(Value) -> Void] = []
init(resolver: (_ resolve: @escaping (Value) -> Void) -> Void) {
resolver(resolve)
}
func then(_ onResolved: @escaping (Value) -> Void) {
callbacks.append(onResolved)
triggerCallbacksIfResolved()
}
func then<T>(_ body: @escaping (Value) -> Promise<T>) -> Promise<T> {
return Promise<T> { resolve in
then { value in
body(value).then(resolve)
}
}
}
func then<T>(_ body: @escaping (Value) -> T) -> Promise<T> {
return then { value in
return Promise<T> { resolve in
resolve(body(value))
}
}
}
private func resolve(value: Value) {
updateState(to: .completed(value))
}
private func updateState(to newState: State<Value>) {
guard case .pending = state else { return }
state = newState
triggerCallbacksIfResolved()
}
private func triggerCallbacksIfResolved() {
guard case let .completed(value) = state else { return }
callbacks.forEach { callback in
callback(value)
}
callbacks.removeAll()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment