 /// Promises start in a *pending* state and either get *fulfilled* with a
 /// value or get *rejected* with an error.
 public final class Promise<T> {
-    private var handlers: Handlers<T>? // nil when finished
-    private var result: Result<T>? // nil when pending
+    private var handlers: Handlers? // nil when finished
+    private var state: State = .pending
 
     // MARK: Creation
 

    }
    
    // MARK: Synchronous Inspection
    
    /// Returns `true` the promise hasn't resolved yet.
    public var isPending: Bool { return _lock.sync { result == nil } }
    
    /// Returns the `value` which promise was `fulfilled` with.
    public var value: T? { return _lock.sync { result?.value } }
    
    /// Returns the `error` which promise was `rejected` with.
    public var error: Error? { return _lock.sync { result?.error } }
}

extension NSLock {
    func sync<T>(_ closure: () -> T) -> T {
        lock(); defer { unlock() };
        return closure()
    }
}

private struct Handlers<T> { // boxed handlers
    var fulfill = ContiguousArray<(T) -> Void>()
    var reject = ContiguousArray<(Error) -> Void>()
}

private enum Result<T> {
    case fulfilled(T), rejected(Error)

    var value: T? {
        if case let .fulfilled(val) = self { return val } else { return nil }
    }

    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()

        let _fulfill: (T) -> Void = { value in queue.async { fulfill(value) } }
        let _reject: (Error) -> Void = { error in queue.async { reject(error) } }

        let state = _register(fulfill: _fulfill, reject: _reject)

        // already resolved
        switch state {
        case .pending: break // obsevers registered
        case let .fulfilled(value): _fulfill(value)
        case let .rejected(error): _reject(error)
        }
    }

    /// Registers observers if pending. Returns state at the time or registration.
    private func _register(fulfill: @escaping (T) -> Void, reject: @escaping (Error) -> Void) -> State {
        lock.lock(); defer { lock.unlock() }
        handlers?.fulfill.append(fulfill)
        handlers?.reject.append(reject)
        return state
    }

    // MARK: Then
