 
       repeat {
         waiter.pointee.next = state.waiters?.assumingMemoryBound(to: Waiter<Value>.self)
-        let newState = Int(bitPattern: waiter) | (state & .resolving)
+        let newState = Int(bitPattern: waiter) | state.state
         if CAtomicsCompareAndExchange(deferredState, &state, newState, .weak, .release, .relaxed)
         { // waiter is now enqueued; it will be deallocated at a later time by notifyWaiters()
           return

{
  static let waiting =   0x0
  static let executing = 0x1
  static let resolving = 0x3
  static let resolved =  0x7

  var isResolved: Bool { return self == .resolved }
  var state: Int { return self & .resolving }
  var waiters: UnsafeMutableRawPointer? { return UnsafeMutableRawPointer(bitPattern: self & ~0x3) }
}

/// An asynchronous computation.

  @discardableResult
  fileprivate func resolve(_ result: Result<Value, Error>) -> Bool
  {
    let original = CAtomicsLoad(deferredState, .relaxed)
    guard original.state != .resolved else { return false }

    let resolved = UnsafeMutablePointer<Result<Value, Error>>.allocate(capacity: 1)
    resolved.initialize(to: result)

    let final = Int(bitPattern: resolved) | .resolved
    var current = CAtomicsLoad(deferredState, .acquire)
    repeat {
      if current.state == .resolved
      {
        resolved.deinitialize(count: 1)
        resolved.deallocate()
        return false
      }
    } while !CAtomicsCompareAndExchange(deferredState, &current, final, .weak, .acqrel, .acquire)

    // This atomic swap operation uses memory order .acqrel.
    // "release" ordering ensures visibility of changes to `resolved` above to another thread.
    // "acquire" ordering ensures visibility of changes to `waitQueue` from another thread.
    // Any atomic load of `waiters` that precedes a possible use of `resolved`
    // *must* use memory order .acquire.

    precondition(current.isResolved == false)
    let waitQueue = current.waiters?.assumingMemoryBound(to: Waiter<Value>.self)
    notifyWaiters(queue, waitQueue, result)

    // This `Deferred` has been resolved
    return true
