不能 return 末端带有 catch 块的 promise 链
Can't return a promise chain with a catch block on the end
这曾经有效,但对于 PromiseKit 版本 6 这...
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request)
.then { r -> Promise<CheckinResponse> in
return .value(r)
}.catch { e in
}
return p
}
...给出...
Cannot convert return expression of type 'PMKFinalizer' to return type
'Promise'
如何添加一个 catch 块并继续 return 到调用函数的链?
您只需要删除 catch
块,如下所示,
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request)
.then { r -> Promise<CheckinResponse> in
return .value(r)
}
return p
}
这里使用捕获块是无关紧要的,因为 error
应该由被调用者处理。
Guarantee
class 是一个包装器 class
来进行可丢弃的 result
调用。所以我们可以创建一个方法来处理 promise
,这样我们就可以使用 .done
回调来使用 result
,如下所示,
extension Promise {
func result() -> Guarantee<T> {
return Guarantee<T>(resolver: { [weak self] (body) in
self?.done({ (result) in
body(result)
}).catch({ (error) in
print(error)
})
})
}
}
现在你可以简单地做,
let request = CheckinRequest()
checkinService.checkIn(request: request).result().done { response in
// Check in response
}
您仍然可以对多个承诺使用链接,如下所示,
checkinService.checkIn(request: request).result().then { (result) -> Promise<Bool> in
// Use reuslt
return .value(true)
}.done { bool in
print(bool)
}.catch { (e) in
print(e)
}
您需要知道的一切都已描述 here,只需滚动到 .catch{ /*…*/ }.finally
部分。
如果您想保持相同的功能,可以使用此解决方案:
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request).then { r -> Promise<CheckinResponse> in
return .value(r)
}
p.catch { e in
}
return p
}
但是,不推荐使用这种模式。 catch
是链终止符。因此,您应该重构您的代码。查看 寻找灵感。
这曾经有效,但对于 PromiseKit 版本 6 这...
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request)
.then { r -> Promise<CheckinResponse> in
return .value(r)
}.catch { e in
}
return p
}
...给出...
Cannot convert return expression of type 'PMKFinalizer' to return type 'Promise'
如何添加一个 catch 块并继续 return 到调用函数的链?
您只需要删除 catch
块,如下所示,
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request)
.then { r -> Promise<CheckinResponse> in
return .value(r)
}
return p
}
这里使用捕获块是无关紧要的,因为 error
应该由被调用者处理。
Guarantee
class 是一个包装器 class
来进行可丢弃的 result
调用。所以我们可以创建一个方法来处理 promise
,这样我们就可以使用 .done
回调来使用 result
,如下所示,
extension Promise {
func result() -> Guarantee<T> {
return Guarantee<T>(resolver: { [weak self] (body) in
self?.done({ (result) in
body(result)
}).catch({ (error) in
print(error)
})
})
}
}
现在你可以简单地做,
let request = CheckinRequest()
checkinService.checkIn(request: request).result().done { response in
// Check in response
}
您仍然可以对多个承诺使用链接,如下所示,
checkinService.checkIn(request: request).result().then { (result) -> Promise<Bool> in
// Use reuslt
return .value(true)
}.done { bool in
print(bool)
}.catch { (e) in
print(e)
}
您需要知道的一切都已描述 here,只需滚动到 .catch{ /*…*/ }.finally
部分。
如果您想保持相同的功能,可以使用此解决方案:
func checkIn(request: CheckinRequest) -> Promise<CheckinResponse> {
let p = checkinService.checkIn(request: request).then { r -> Promise<CheckinResponse> in
return .value(r)
}
p.catch { e in
}
return p
}
但是,不推荐使用这种模式。 catch
是链终止符。因此,您应该重构您的代码。查看