mxcl / PromiseKit

Promises for Swift & ObjC.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throwing errors is breaking promise return and returning multiple promise

Yetispapa opened this issue · comments

commented

Version: 6.13.1
Installed by CocoaPods

I have the following function which should be creating a new class called Objectand returning it as a promise. The function self.objectCreator.add(object) is writing the model to a database and after that it should also be connected to another model called OtherObject with . self.otherObjectCreator.add(object: object).

I don't get any errors when I remove the guard let object = object else line. But I have to make a optional check cause the otherObjectCreator.add needs a non optional parameter.

I read through the troubleshooting and could manage to fix some errors by returning the promise self.otherObjectCreator.add(object: object) but any way. I thought this is a normal case of using PromiseKit? Is there something wrong with seal.fulfill(self.queryFromId(id: uuid))?

Futhermore I need to return two promises. How can I achieve this? (see the line "so this should be somehow return the two promises")

Hope you can help me here or give me an advice how to proceed here

func newObject() -> Promise<Object>{
    let object = Object()

    return firstly {
        self.objectCreator.add(object)
    }
    .then { object in // <- got exception: Unable to infer complex closure return type; add explicit type to disambiguate
        
        guard let object = object else {
            throw ObjectError()
        }

        // returns Promise of OtherObject
        return self.otherObjectCreator.add(object: object)
        
    // so this should be somehow return the two promises 
    return Promises(self.otherObjectCreator.addObject(object: object), Promise.init(object))

    }
    .then { (otherObject, object) in
    return object
    }
}

This is the function body of self.objectCreator.add. The function body of self.otherObjectCreator.addObject is more or less the same.

func add(_ model: Object) -> Promise<Object?>{
        
            let uuid = UUID().uuidString
            
            model.id = uuid

      return Promise<T?> { seal in

          self.transaction(write: { (database) in
              database.add(mode)
          }) { result in
              
              switch(result){
              case .success(_):
                 // is returning the type `Object?`
                  seal.fulfill(self.queryFromId(id: uuid))
              case .failure(let error):
                  seal.reject(error)
              }
          }
          
      }
      
  }

Unable to infer complex closure return type; add explicit type to disambiguate

As the troubleshooting guide and the error both say, provide the closure return type:

.then { object -> Promise<OtherObject> in

so this should be somehow return the two promises

You didn’t specify what you were trying to achieve, so I can only guess. But probably you want when().

return when(fulfilled: self.otherObjectCreator.addObject(object: object), Promise.init(object))

You should read our documentation.