I may be missing the obvious-- but how can I create a resolved instance of Promise<Void> ?
I know there is public func asVoid() but that assumes you have a Promise. What if I simply want a already resolved Promise without value?
required public init(value: T) doesn't work and Promise() doesn't appear to be working either.
Thanks for your help. Sorry for the possibly silly question!
Promise(value: ())
Also, it's not a stupid question, I was initially confused about how to do this too, and I wrote the code. Promise() used to work, but we elected to be more “Swifty” with v4 and thus the value parameter is now mandatory, which means the Void initializer ends up being odd.
OMG, I come up with the following code, before found this:
let voidValue: Void = {return}();
return Promise<Void>(value: voidValue)
KISS!
@mxcl I return void promises a lot in my code and I find both syntaxes – Promise(value: Void()) and Promise(value: ()) – to be confusing. I ended up adding Promise.void. Would it be appropriate to have that in the library? I'm thinking about something like this:
extension Promise {
static var void: Promise<Void> {
return Promise<Void>(value: ())
}
}
And how I use it:
func doSomething() -> Promise<Void> {
return .void
}
Hmm I tried something like this:
extension Promise {
static var void: Promise<Void> {
return Promise<Void>(value: ())
}
}
And Swift wouldn't compile it due to it requiring T to be inferred. I must have been doing it wrong.
Could you PR it?
How about creating an extension for only
Though the deprecated init() function would cause a problem.
extension Promise where T == Void
{
convenience init()
{
self.init(value: ())
}
}
Maybe this will work since Swift 3.1, since that syntax was only just added. PR welcome, if it works and all tests pass.
For future searchers out there, the current way (Swift 4) to do create a fulfilled promise for a Promise<Void> is:
Promise<Void>.value(())
or for example:
doSomething().then { result -> Promise<Void> in
if result.somethingIsIndicated {
return doSomethingElse()
} else {
return Promise.value(())
}
}
@RobinDaugherty you can do Promise().
This information is in our documentation.
This is since Swift 3.2, which allowed generic specializations, thus we specialized Promise<Void>.
Thanks @mxcl that does work, but I don't think the code is as readable. On the other hand, I can't find this in the documentation, can you point me to that?
Most helpful comment
Promise(value: ())