title: Chaining promises on PromiseKit
body: <p>I'm trying to chain a number of promises that need to be resolved before returning.</p>
  
  <p>In my case, for each element of databaseResult I need to fetch some data with a method that returns a promise.</p>
  
  <p>Once I've fetched the data for every single element of the array I need to return to the calling method.     </p>
  
  <pre><code>var toReturn = [MatchModel]()
  //get my array of data
  let databaseResults = MatchDatabaseManager.getMatchList();
  
      //not sure what I'm doing
      var promise = dispatch_promise{ 0 }
      if(databaseResults.count &gt; 0) {
          return Promise { fulfill, reject in
  
              for index in 0..&lt;databaseResults.count {
                  print(index)
                  promise = promise.then { y -&gt; Promise&lt;Int&gt; in
                      //Fetch the data I need ...
  
                    DataProvider.getUserProfileWithUserId(
                    (databaseResults[y].partnerUserProfile?.userId)!)
                    .then {(model) in {
                    //and use it to create the data I need to return
                          toReturn.append(MatchModel(realmModel:            
                           databaseResults[y], partnerProfile: model))
  
                          }
                      }
  
                      return dispatch_promise { index }
                  }
              }
              //Once all the promises are fulfilled, toReturn contains the data I need and I can return it
              promise.then{ x in {
                  fulfill(toReturn)
                  }
              }
          }
      }
  </code></pre>
  
  <p>If I run this I get</p>
  
  <pre><code> PromiseKit: Pending Promise deallocated! This is usually a bug
  </code></pre>
  
  <p>I have very little experience with PromiseKit and documentation / exaples are scarce, so I have no idea what I'm missing here.</p>