Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fulfill nil promise swift

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 '((, _) -> _)'

like image 505
Cesar Oyarzun Avatar asked Jul 05 '16 22:07

Cesar Oyarzun


1 Answers

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)

like image 50
Kametrixom Avatar answered Sep 22 '22 18:09

Kametrixom