     var reject = [(Error) -> Void]()
 }
 
-private enum State<T> {
-    case pending(Handlers<T>), fulfilled(T), rejected(Error)
-}
+private enum Result<T> {
+    case fulfilled(T), rejected(Error)
+    /// Returns a `value` if the result is success.
+
+    var value: T? {
+        if case let .fulfilled(val) = self { return val } else { return nil }
+    }
+
+    /// Returns an `error` if the result is failure.
+    var error: Error? {
+        if case let .rejected(err) = self { return err } else { return nil }
+    }
+}
+
+// We use the same lock across different tokens because the design of Promise
+// prevents potential issues. For example, closures registered with a Promise
+// are never executed inside a lock.
+private let _lock = NSLock()

    // MARK: Synchronous Inspection
    
    /// Returns `true` the promise hasn't resolved yet.
    public var isPending: Bool { if case .pending(_) = _state { return true } else { return false } }
    
    /// Returns the `value` which promise was `fulfilled` with.
    public var value: T? { if case let .fulfilled(val) = _state { return val } else { return nil } }
    
    /// Returns the `error` which promise was `rejected` with.
    public var error: Error? { if case let .rejected(err) = _state { return err } else { return nil } }
    
    private var _state: State<T> {
        lock.lock(); defer { lock.unlock() }
        return state
    }
}


/// Promises start in a *pending* state and either get *fulfilled* with a
/// value or get *rejected* with an error.
public final class Promise<T> {
    // previous `State` based approach was easier to reason about but was
    // harder to implement in a performant way.
    private var handlers: Handlers<T>? // nil when finished
    private var result: Result<T>? // nil when pending

    // MARK: Creation

