     // MARK: Synchronous Inspection
     
     /// Returns `true` the promise hasn't resolved yet.
-    public var isPending: Bool { if case .pending(_) = _state { return true } else { return false } }
+    public var isPending: Bool { return _lock.sync { result == nil } }
     
     /// Returns the `value` which promise was `fulfilled` with.
-    public var value: T? { if case let .fulfilled(val) = _state { return val } else { return nil } }
+    public var value: T? { return _lock.sync { result?.value } }
     
     /// 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
+    public var error: Error? { return _lock.sync { result?.error } }
+}
+
+extension NSLock {
+    func sync<T>(_ closure: () -> T) -> T {
+        lock(); defer { unlock() }
+        return closure()
     }
 }
 

    var reject = [(Error) -> Void]()
}

private enum State<T> {
    case pending(Handlers<T>), fulfilled(T), rejected(Error)
}

    /// thread. You should start an asynchronous task and call either `fulfill`
    /// or `reject` when it completes.
    public init(_ closure: (_ fulfill: @escaping (T) -> Void, _ reject: @escaping (Error) -> Void) -> Void) {
        handlers = Handlers<T>()
        closure({ self._fulfill($0) }, { self._reject($0) })
    }
    
    private func _fulfill(_ value: T) {
        if let handlers = _resolve(.fulfilled(value)) {
            handlers.fulfill.forEach { $0(value) } // called outside of lock
        }
    }

    private func _reject(_ error: Error) {
        if let handlers = _resolve(.rejected(error)) {
            handlers.reject.forEach { $0(error) } // called outside of lock
        }
    }

    // Return handlers to call if promise was resolved.
    private func _resolve(_ result: Result<T>) -> Handlers<T>? {
        _lock.lock(); defer { _lock.unlock() }
        guard self.result == nil else { return nil } // already resolved
        self.result = result
        let handlers = self.handlers
        self.handlers = nil
        return handlers
    }

    /// Creates a promise fulfilled with a given value.
    public init(value: T) { result = .fulfilled(value) }

    /// Creates a promise rejected with a given error.
    public init(error: Error) { result = .rejected(error) }

    // MARK: Callbacks
    
