PromiseKit 分支承诺
PromiseKit branching promise
假设您的 promise 链中有一个分支,它可以 return 什么都没有,也可以是 AnyObject promise。您会指定什么作为 'then' 闭包的 return 类型?例如:
func sample() -> Promise<AnyObject> {
return Promise { fulfill, reject in
fulfill(1)
}
.then { _ -> Void in
if false {
return Promise { fulfill, reject in
fulfill(0)
}
}
}
}
如果我将 Void 作为 'then' 闭包的 return 类型,我会得到一个段错误;如果我将 Promise 设为 return 类型,则会出现错误:
missing return in a closure expected to return Promise<AnyObject>
有什么建议吗?
谢谢
根据代码示例,我认为没有理由 return AnyObject。如果你想可选地 return Void 或 Object,那么做出包含可选的承诺。
func sample() -> Promise<AnyObject?> {
return Promise { fulfill, reject in
functionForGettingObjectWithCallback() { result: AnyObject? in
fulfill(result)
}
}
}
假设您的 promise 链中有一个分支,它可以 return 什么都没有,也可以是 AnyObject promise。您会指定什么作为 'then' 闭包的 return 类型?例如:
func sample() -> Promise<AnyObject> {
return Promise { fulfill, reject in
fulfill(1)
}
.then { _ -> Void in
if false {
return Promise { fulfill, reject in
fulfill(0)
}
}
}
}
如果我将 Void 作为 'then' 闭包的 return 类型,我会得到一个段错误;如果我将 Promise 设为 return 类型,则会出现错误:
missing return in a closure expected to return Promise<AnyObject>
有什么建议吗?
谢谢
根据代码示例,我认为没有理由 return AnyObject。如果你想可选地 return Void 或 Object,那么做出包含可选的承诺。
func sample() -> Promise<AnyObject?> {
return Promise { fulfill, reject in
functionForGettingObjectWithCallback() { result: AnyObject? in
fulfill(result)
}
}
}