在 PromiseKit 6 中返回 void
Returning void in PromiseKit 6
这就是我使用 PromiseKit 4.5 时所做的工作
api.getUserFirstName().then { name -> Void in
print(name)
}
getUserFirstName()
returns一个Promsise<String>
。我更新到 PromiseKit 6,现在会抛出一个错误:
Cannot convert value of type '(_) -> Void' to expected argument type '(_) -> _'
这条错误消息对我来说意义不大。我该如何解决这个问题?
编辑:所以这似乎解决了它,但我对发生的事情知之甚少:
api.getUserFirstName().compactMap { name in
print(name)
}
现在then()
和compactMap()
有什么区别?
根据PromiseKit 6.0 Guide then
拆分为then
、done
和map
then
提供了先前的承诺值,需要您 return 承诺。
done
提供了先前的承诺值,并且 return 是一个 Void 承诺(这是链使用的 80%)
map
提供了先前的承诺值,并要求您 return 一个 non-promise,即。一个值。
为什么会这样?正如开发人员所说:
With PromiseKit our then
did multiple things, and we relied on Swift to infer the correct then
from context. However with multiple line then
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 dreaded cannot 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.
所以在你的情况下可能需要使用 done
func WhosebugExample() {
self.getUserFirstName().done { name -> Void in
print(name)
}
}
func getUserFirstName() -> Promise<String> {
return .value("My User")
}
compactMap
让你在 returned 时得到错误传输。
firstly {
URLSession.shared.dataTask(.promise, with: url)
}.compactMap {
try JSONDecoder().decode(Foo.self, with: [=11=].data)
}.done {
//…
}.catch {
// though probably you should return without the `catch`
}
查看更多信息
compactMap
已重命名为 flatMap
see discussions here
这就是我使用 PromiseKit 4.5 时所做的工作
api.getUserFirstName().then { name -> Void in
print(name)
}
getUserFirstName()
returns一个Promsise<String>
。我更新到 PromiseKit 6,现在会抛出一个错误:
Cannot convert value of type '(_) -> Void' to expected argument type '(_) -> _'
这条错误消息对我来说意义不大。我该如何解决这个问题?
编辑:所以这似乎解决了它,但我对发生的事情知之甚少:
api.getUserFirstName().compactMap { name in
print(name)
}
现在then()
和compactMap()
有什么区别?
根据PromiseKit 6.0 Guide then
拆分为then
、done
和map
then
提供了先前的承诺值,需要您 return 承诺。done
提供了先前的承诺值,并且 return 是一个 Void 承诺(这是链使用的 80%)map
提供了先前的承诺值,并要求您 return 一个 non-promise,即。一个值。
为什么会这样?正如开发人员所说:
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.
所以在你的情况下可能需要使用 done
func WhosebugExample() {
self.getUserFirstName().done { name -> Void in
print(name)
}
}
func getUserFirstName() -> Promise<String> {
return .value("My User")
}
compactMap
让你在 returned 时得到错误传输。
firstly {
URLSession.shared.dataTask(.promise, with: url)
}.compactMap {
try JSONDecoder().decode(Foo.self, with: [=11=].data)
}.done {
//…
}.catch {
// though probably you should return without the `catch`
}
查看更多信息
compactMap
已重命名为 flatMap
see discussions here