问题
This is what I had working with PromiseKit 4.5
api.getUserFirstName().then { name -> Void in
print(name)
}
getUserFirstName()
returns a Promsise<String>
. I updated to PromiseKit 6 and this now throws an error:
Cannot convert value of type '(_) -> Void' to expected argument type '(_) -> _'
This error message makes little sense to me. How do I fix this?
EDIT: So this seems to fix it, but I have little understanding as to what's happening with this:
api.getUserFirstName().compactMap { name in
print(name)
}
What's the difference now between then()
and compactMap()
?
回答1:
In according with PromiseKit 6.0 Guide then
was split into then
, done
and map
then
is fed the previous promise value and requires you return a promise.done
is fed the previous promise value and returns a Void promise (which is 80% of chain usage)map
is fed the previous promise value and requires you return a non-promise, ie. a value.
Why that was happend? As said developers:
With PromiseKit our
then
did multiple things, and we relied on Swift to infer the correctthen
from context. However with multiple linethen
s it would fail to do this, and instead of telling you that the situation was ambiguous it would invent some other error. Often the dreadedcannot convert T to AnyPromise
. We have a troubleshooting guide to combat this but I believe in tools that just work, and when you spend 4 years waiting for Swift to fix the issue and Swift doesn’t fix the issue, what do you do? We chose to find a solution at the higher level.
So probably in your case needs to use done
func stackOverflowExample() {
self.getUserFirstName().done { name -> Void in
print(name)
}
}
func getUserFirstName() -> Promise<String> {
return .value("My User")
}
compactMap
lets you get error transmission when nil is returned.
firstly {
URLSession.shared.dataTask(.promise, with: url)
}.compactMap {
try JSONDecoder().decode(Foo.self, with: $0.data)
}.done {
//…
}.catch {
// though probably you should return without the `catch`
}
See more info at release guide
compactMap
was renamed to flatMap
see discussions here
来源:https://stackoverflow.com/questions/49147115/returning-void-in-promisekit-6