title: How to refactor swift callback to promise with PromiseKit
body: <p>I am attempting my first foray into swift promises with PromiseKit, after some experience with doing them with bluebird in node. My original service function for authentication with a callback looks like this:</p>
  
  <pre><code>private func requestNewToken(email : String, password: String,
  completion: (success: Bool, token: String?, userId : String?, message: String? ) -&gt; Void) {
  
  Alamofire.request(Router.Authenticate(email, password).URLRequest)
      .responseJSON { request, response, result in
          switch result {
          case .Success(let data):
              let jsonData = JSON(data)
  
              let success = jsonData["success"].bool!
              let message = jsonData["message"].string!
  
              if(success) {
                  let token = jsonData["token"].string
                  let userId = jsonData["_id"].string
  
                  self.deleteSavedUser()
                  self.saveUserToCoreData(email, password: password, token: token!, userId: userId!)
                  completion(success: true, token: token, userId: userId, message:  message)
                  return
              }
  
              completion(success: false, token: nil, userId: nil, message: message)
              print("Token request failed with error: \(message)")
  
          case .Failure(_, let error):
              print("Request failed with error: \(error)")
              completion(success: false, token: nil, userId : nil, message: nil)
          }
  }
  }
  </code></pre>
  
  <p>I'm attempting to rewrite to something like this:</p>
  
  <pre><code>private func requestNewTokenPromise(email : String, password:
  String) -&gt; Promise&lt;UserLoginData?&gt;{         
   return Promise{ fulfill, reject in             
     Alamofire.request(Router.Authenticate(email,password).URLRequest)
          .responseJSON { request, response, result in
              switch result {
              case .Success(let data):
                  let jsonData = JSON(data)
                  fulfill(jsonData)
              case .Failure(_, let error):
                  print("Request failed with error: \(error)")
                  reject(error)
              }
          }
  }.then { json in
  
              let success = json["success"].bool!
              let message = json["message"].string!
  
              if(success) {
                  let token = json["token"].string
                  let userId = json["_id"].string
  
                  self.deleteSavedUser()
                  self.saveUserToCoreData(email, password: password, token: token!, userId: userId!) //sets the class's savedUser property
  
                  return Promise(self.savedUser);
              }
  
              return Promise(nil);            
   }  
  }
  </code></pre>
  
  <p>However, I get an error on my then line:</p>
  
  <pre><code>AuthenticationService.swift:147:6: Cannot convert return expression of type 'Promise&lt;AnyObject?&gt;' 
  (aka 'Promise  Optional&lt;AnyObject&gt;&gt; ') to return type 'Promise&lt;UserLoginData?&gt;' (aka 'Promise&lt;Optional&lt;UserLoginData&gt;&gt;')
  </code></pre>
  
  <p>I had thought by resolving the promise with Promise() that I'd be fulfilling the original promise type. Where am I going wrong?</p>