     /// 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) })
+        closure({ self._fulfill($0) }, { self._reject($0) }) // retain self
     }
     
     private func _fulfill(_ value: T) {
-        if let handlers = _resolve(.fulfilled(value)) {
-            handlers.fulfill.forEach { $0(value) } // called outside of lock
-        }
+        _resolve(.fulfilled(value))?.fulfill.forEach { $0(value) }
     }
 
     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.
+        _resolve(.rejected(error))?.reject.forEach { $0(error) }
+    }
+
+    // Return handlers to call if promise was resolved. Returns nil otherwise.
     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
+        return handlers // returns handlers to be used outside of critical section
     }
 
     /// Creates a promise fulfilled with a given value.

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

        if let result = _register(fulfill: _fulfill, reject: _reject) {
            // already resolved
            switch result {
            case let .fulfilled(value): _fulfill(value)
            case let .rejected(error): _reject(error)
            }
        }
    }

    /// Either registered observers of returns result if resolved.
    private func _register(fulfill: @escaping (T) -> Void, reject: @escaping (Error) -> Void) -> Result<T>? {
        _lock.lock(); defer { _lock.unlock() }
        handlers?.fulfill.append(fulfill)


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 }
    }
