title: PromiseKit dependent promises result in early deallocation
body: <p>I'm trying to tame some callback hell pyramid of doom code using <a href="http://promisekit.org/" rel="nofollow noreferrer">PromiseKit</a>. </p>
  
  <p>To do this I wrap my asynchronous code in promises, but depending on how I return dependent promises, I encounter problems. If I unwrap the promise and fulfill/reject then all is well, though more verbose than I'd like. If I return a new dependent promise, then I get an early allocation and the promises are silently broken.</p>
  
  <p>I realise this might not be idiomatic PromiseKit, which appears to be </p>
  
  <pre><code>{ a }.then { b }.then { c }  // in temporal order, take that, callbacks!
  </code></pre>
  
  <p>but as part of this work it's convenient for me to refactor with functions <code>Promise&lt;A&gt; -&gt; Promise&lt;B&gt;</code>, and I don't understand why I must unwrap at every step. Does anyone know?</p>
  
  <p>Here is some simplified code that reproduces the problem. Try to imagine that there's a good reason that <code>badStringFromInt</code> can't fulfil immediately.</p>
  
  <pre><code>func badStringFromInt(_ intPromise: Promise&lt;Int&gt;) -&gt; Promise&lt;String&gt; {
      return Promise { _, reject in
          intPromise.then { i -&gt; Promise&lt;String&gt; in
              return Promise { fulfill, _ in
                  fulfill("\(i)")
              }
          }.catch { error in
              reject(error)
          }
      }
  }
  
  func goodStringFromInt(_ intPromise: Promise&lt;Int&gt;) -&gt; Promise&lt;String&gt; {
      return Promise { fulfill, reject in
          intPromise.then { i in
              fulfill("\(i)")
          }.catch { error in
              reject(error)
          }
      }
  }
  
  func getInt() -&gt; Promise&lt;Int&gt; {
      return Promise{ fulfill, reject in
          fulfill(5)
      }
  }
  
  func doIt() {
      // "then" never called, and this is logged:
      // PromiseKit: Pending Promise deallocated! This is usually a bug
      badStringFromInt(getInt()).then { s in
          print("bad string is :" + s)
      }.catch { error in
          print("error: \(error)")
      }
  
      // all good!
      goodStringFromInt(getInt()).then { s in
          print("good string is :" + s)
      }.catch { error in
          print("error: \(error)")
      }
  }
  </code></pre>