6

I want to fulfill a promise with nil but I get error message that I cant here is my code

public static func getCurrentDevice() -> Promise<Device>{
    if let identity:[String:AnyObject] = auth?.get("identity") as! [String:AnyObject] {
        if let uuididentity = identity["uuid"]{
         return Promise { fulfill, reject in
            Alamofire.request( Router.getDevice(uuididentity as! String) )
                .responseObject { (response: Response<Device, NSError>) in
                    switch response.result{
                    case .Success(let data):
                        fulfill(data)
                    case .Failure(let error):
                        return reject(error)
                    }
            }
        }
    }
}
return Promise { fulfill, reject in
        fulfill(nil)
    }
}

I get compiler error Cannot invoke initializer for type 'Promise<>' with an argument list of type '((, _) -> _)'

Cesar Oyarzun
  • 169
  • 2
  • 8

2 Answers2

6

If the promise doesn't return a value you should use () aka Void:

return Promise { fulfill, reject in
    fulfill(())
}

If this doesn't work (I didn't test it) you could try annotate it:

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

(Note that () is the only value of type () aka Void)

Kametrixom
  • 14,673
  • 7
  • 45
  • 62
0

Here you go. The question mark makes it for you in the return of the method.

public static func getCurrentDevice() -> Promise<Device?> {
        //... logic here
        let isDeviceEmpty: Bool

        if isDeviceEmpty {
            fulfill(nil)
        } else {
            fulfill(device)
        }
    }
Balázs Papp
  • 51
  • 1
  • 3