 public class PromiseSource<Value, Error> {
   typealias ResultHandler = (Result<Value, Error>) -> Void
 
+  private let lock = NSLock()
+
   private var handlers: [(Result<Value, Error>) -> Void] = []
   internal let dispatchKey: DispatchSpecificKey<Void>
   internal let dispatchMethod: DispatchMethod

  ///
  /// When called on a PromiseSource that is already Resolved or Rejected, the call is ignored.
  public func resolve(_ value: Value) {

    resolveResult(.value(value))
  }


  ///
  /// When called on a PromiseSource that is already Resolved or Rejected, the call is ignored.
  public func reject(_ error: Error) {
    resolveResult(.error(error))
  }

  internal func resolveResult(_ result: Result<Value, Error>) {
    lock.lock(); defer { lock.unlock() }

    switch state {
    case .unresolved:
      state = result.state

      // Call all previously scheduled handlers
      callHandlers(result, handlers: handlers, dispatchKey: dispatchKey, dispatchMethod: dispatchMethod)

      // Cleanup
      handlers = []
    default:
      break
    }
  }

  // MARK: Adding result handlers

  internal func addOrCallResultHandler(_ handler: @escaping (Result<Value, Error>) -> Void) {
    lock.lock(); defer { lock.unlock() }

    switch state {
    case .unresolved:
