Swift 的 PromiseKit:终止承诺链
PromiseKit with Swift: terminate chain of promises
我正在尝试将 PromiseKit 与 Swift 一起使用。我不是很熟悉它,关于它在 Swift 中的用法似乎也没有太多信息。
我似乎不知道如何终止承诺链。只要最后一个(终端)then
块包含单个语句,一切都很好:
firstly {
// ...
}.then { obj in
self.handleResult(obj)
}.catch { error in
self.handleError(error)
}
但是,如果我尝试添加另一条语句,编译器会报错:
firstly {
// ...
}.then { obj in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in // compiler error: Missing return in a closure expected to return 'AnyPromise'
self.handleError(error)
}
显然,解决方案是 return 另一个承诺,但在接线端子中没有意义。还有什么我可以做的吗?
根据 http://promisekit.org/PromiseKit-2.0-Released/,在 Swift 编译器问题 部分下:
The Swift compiler will often error with then. To figure out the
issue, first try specifying the full signature for your closures:
foo.then { x in
doh()
return bar()
}
// will need to be written as:
foo.then { obj -> Promise<Type> in
doh()
return bar()
}
// Because the Swift compiler cannot infer closure types very
// well yet. We hope this will be fixed.
// Watch out for one-line closures though! Swift will
// automatically infer the types, which may confuse you:
foo.then {
return bar()
}
因此您必须将代码更改为:
firstly {
// ...
}.then { obj -> Promise<WhateverTypeDoSomethingDifferentPromises> in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}
或者你可以使用obj -> Void
停止链
改为:
firstly {
// ...
}.then { obj -> Void in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}
在 Swift 5.1 版中,您可以使用 Combine Framework 来解决这个问题,您可以使用模式 Future。这与 Promise 使用的相同。您的代码将如下所示:
final class Future<Output, Failure> where Failure : Error
所以只是提醒您注意 Swift 和新框架中的承诺。
在 PromiseKit 6.10.0 中,如果您不想使用 Thenable
:
继续链,请使用 done
而不是 then
firstly {
// ...
}.done { obj in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}
我正在尝试将 PromiseKit 与 Swift 一起使用。我不是很熟悉它,关于它在 Swift 中的用法似乎也没有太多信息。
我似乎不知道如何终止承诺链。只要最后一个(终端)then
块包含单个语句,一切都很好:
firstly {
// ...
}.then { obj in
self.handleResult(obj)
}.catch { error in
self.handleError(error)
}
但是,如果我尝试添加另一条语句,编译器会报错:
firstly {
// ...
}.then { obj in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in // compiler error: Missing return in a closure expected to return 'AnyPromise'
self.handleError(error)
}
显然,解决方案是 return 另一个承诺,但在接线端子中没有意义。还有什么我可以做的吗?
根据 http://promisekit.org/PromiseKit-2.0-Released/,在 Swift 编译器问题 部分下:
The Swift compiler will often error with then. To figure out the issue, first try specifying the full signature for your closures:
foo.then { x in
doh()
return bar()
}
// will need to be written as:
foo.then { obj -> Promise<Type> in
doh()
return bar()
}
// Because the Swift compiler cannot infer closure types very
// well yet. We hope this will be fixed.
// Watch out for one-line closures though! Swift will
// automatically infer the types, which may confuse you:
foo.then {
return bar()
}
因此您必须将代码更改为:
firstly {
// ...
}.then { obj -> Promise<WhateverTypeDoSomethingDifferentPromises> in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}
或者你可以使用obj -> Void
停止链
改为:
firstly {
// ...
}.then { obj -> Void in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}
在 Swift 5.1 版中,您可以使用 Combine Framework 来解决这个问题,您可以使用模式 Future。这与 Promise 使用的相同。您的代码将如下所示:
final class Future<Output, Failure> where Failure : Error
所以只是提醒您注意 Swift 和新框架中的承诺。
在 PromiseKit 6.10.0 中,如果您不想使用 Thenable
:
done
而不是 then
firstly {
// ...
}.done { obj in
self.handleResult(obj)
self.doSomethingDifferent(obj)
}.catch { error in
self.handleError(error)
}