PromiseKit 3.0 链接
PromiseKit 3.0 chaining
我正在尝试编写一个 return 承诺的函数:
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
我在最后一个 then 语句中出错:
Declared closure result 'Void' (aka '()') is incompatible with contextual type 'AnyPromise'
我的印象是 'then' 无论如何都应该隐含地 return 承诺;我的想法错了吗?我应该 return 像这样明确地承诺吗?:
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Promise<AnyObject> in
debugPrint("foo")
return Promise(1)
}
}
谢谢
A then
调用 returns 无论您在调用中指定什么,在您的第一个示例中为 Void
。
你第二次尝试那里,你更接近了,但是你返回了一个不相关的 Promise
,你必须用 1
第二次完成它。
试试这个代码:
func sample() -> Promise<AnyObject> {
return Promise<AnyObject> { fulfill, reject in
return Promise<AnyObject>(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
}
这会将第二个 Promise
嵌入到第一个中,因此现在您的 then
将按顺序 运行 以及您添加到函数返回的承诺中的任何其他人当它在您的代码中的其他任何地方被调用时。
由 then(_:)
编写的承诺 return 匹配闭包的 return 值。
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
让我修改你的方法。
func sample() -> Promise<AnyObject> {
let p1: Promise<AnyObject> = Promise(1)
let p2: Promise<Void> = p1.then { _ -> Void in
debugPrint("foo")
}
let p3: Promise<Void> = p2.then { _ -> Void in
debugPrint("foo")
}
return p3
}
您现在可以看到 Promise<AnyObject>
的预期 return 类型与 Promise<Void>
的实际 return 类型不匹配。
如果你想要一个方法returnPromise<AnyObject>
,那么promise链中的最后一个promise必须returnAnyObject
.
func sample() -> Promise<AnyObject> {
return firstly { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}.then { _ -> AnyObject in
1
}
}
我正在尝试编写一个 return 承诺的函数:
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
我在最后一个 then 语句中出错:
Declared closure result 'Void' (aka '()') is incompatible with contextual type 'AnyPromise'
我的印象是 'then' 无论如何都应该隐含地 return 承诺;我的想法错了吗?我应该 return 像这样明确地承诺吗?:
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Promise<AnyObject> in
debugPrint("foo")
return Promise(1)
}
}
谢谢
A then
调用 returns 无论您在调用中指定什么,在您的第一个示例中为 Void
。
你第二次尝试那里,你更接近了,但是你返回了一个不相关的 Promise
,你必须用 1
第二次完成它。
试试这个代码:
func sample() -> Promise<AnyObject> {
return Promise<AnyObject> { fulfill, reject in
return Promise<AnyObject>(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
}
这会将第二个 Promise
嵌入到第一个中,因此现在您的 then
将按顺序 运行 以及您添加到函数返回的承诺中的任何其他人当它在您的代码中的其他任何地方被调用时。
由 then(_:)
编写的承诺 return 匹配闭包的 return 值。
func sample() -> Promise<AnyObject> {
return Promise(1)
.then { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}
}
让我修改你的方法。
func sample() -> Promise<AnyObject> {
let p1: Promise<AnyObject> = Promise(1)
let p2: Promise<Void> = p1.then { _ -> Void in
debugPrint("foo")
}
let p3: Promise<Void> = p2.then { _ -> Void in
debugPrint("foo")
}
return p3
}
您现在可以看到 Promise<AnyObject>
的预期 return 类型与 Promise<Void>
的实际 return 类型不匹配。
如果你想要一个方法returnPromise<AnyObject>
,那么promise链中的最后一个promise必须returnAnyObject
.
func sample() -> Promise<AnyObject> {
return firstly { _ -> Void in
debugPrint("foo")
}.then { _ -> Void in
debugPrint("foo")
}.then { _ -> AnyObject in
1
}
}