google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 294 forks source link

Cannot convert value of type 'Promise<Any>' to expected argument type '(Any) throws -> Void' Error in Swift #129

Open Darrow8 opened 4 years ago

Darrow8 commented 4 years ago

getAllUsers(gsuiteName: gsuiteName).then(uploadUsers(arr1: currentUsersEM,arr2: currentUsersUS)).then(newScreen())

Am trying to use this library for the first time and am running into this error when chaining promises together:

Cannot convert value of type 'Promise' to expected argument type '(Any) throws -> Void' Error in Swift

Here is the code for getAllUsers() and uploadUsers(): ` func getAllUsers(gsuiteName: String) -> Promise{

    Database.database().reference().child("Messages").child(gsuiteName).child("USERS").observe(.value) { (snapshot) in
        //when(
        for child in snapshot.children{
            self.currentUsersEM.append((child as AnyObject).value as String)
            self.currentUsersUS.append((child as AnyObject).key as String)
        }
        self.currentUsersEM.append((Auth.auth().currentUser?.email)!)
        self.currentUsersUS.append(self.username.text!)
    }
    return Promise {
        return nil
    }
}
func uploadUsers(arr1: Array<String>, arr2: Array<String>) -> Promise<Any>{
    for usCount in 0..<(arr1.count){
        print("USERNAME\(arr1[usCount] as! String)")
        print("EMAIL\(arr2[usCount] as! String)")
        //Database.database().reference().child("Messages").child(gsuiteName).child("USERS").setValue([currentUsersUS[usCount] as! String :currentUsersEM[usCount] as! String])
    }
    return Promise{
        return nil
    }
}
func newScreen(){
    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
    changeRequest?.displayName = self.username.text
    changeRequest?.commitChanges { (error) in
        self.performSegue(withIdentifier: "goToChat", sender: self)
        //}
    }
}`
nsoojin commented 4 years ago

Hi Darrow8,

I recommend taking a closer look at Creating promises.

return Promise { 
  return nil
}

This is not a valid declaration.

It should Either be

return Promise { () -> Any
  return yourAnyObject
}

or

return Promise<Any> { fulfill, reject in
  fulfill(yourAnyObject)
}

If you want to return nil, then your Promise type should be Promise<Any?>.

p.s. Seeing by your code, you don't want to return anything but would just like to be notified when done. So why not use Promise<Void> type for that. Also, I think what you want is this. Put the code you want to run asynchronously inside the closure.

func uploadUsers(arr1: Array<String>, arr2: Array<String>) -> Promise<Void> {
  return Promise { () -> Void in
    for usCount in 0..<(arr1.count){
      print("USERNAME\(arr1[usCount] as! String)")
      print("EMAIL\(arr2[usCount] as! String)")
    }
    return ()
  }
}