如何在PromiseKit中设置when(fulfilled :)? [英] How to set up when(fulfilled:) in PromiseKit?
问题描述
我设置了一个返回Promise<PFObject>
的函数.我想在PromiseKit的when(fulfilled:)
功能中使用此功能,但是每当尝试这样做时,都会出现错误.这是返回Promise<PFObject>
的函数:
I have a function set up to return a Promise<PFObject>
. I would like to use this function in PromiseKit's when(fulfilled:)
functionality, but whenever I try to do so, I get an error. Here is the function which returns the Promise<PFObject>
:
func Query() -> Promise<PFObject>{
return Promise{ fulfill, reject in
let linkQueryy = PFUser.query()
linkQueryy?.findObjectsInBackground(block: { (objectss, error) in
if let objects = objectss{
for object in objects{
fulfill(object)
}
}
})
}
}
如您所见,该函数在实现时会返回Promise.因此,我尝试在我的viewDidLoad()
中设置一个when
语句,如下所示:
As you can see, the function returns the Promise upon fulfillment. Thus, I tried to set up a when
statement in my viewDidLoad()
as follows:
override func viewDidLoad() {
super.viewDidLoad()
when(fulfilled: Query()).then{
//do more asynch stuff
}
}
但是,我得到一个错误,即xcode无法使用参数列表类型为'(fulfilled:Promise<PFObject>
)'调用'when',".我不知道如何解决此问题,因为我认为我已正确设置了它. when
需要一个承诺,而我正在给它一个承诺,所以我不确定该怎么做.
However, I get the error that xcode cannot "invoke 'when' with an argument list type of '(fulfilled: Promise<PFObject>
)'". I do not know how to fix this as I thought I had it set up correctly. The when
needs a promise, and I am giving it one so I am not sure what to do.
推荐答案
尝试如下:
when(fulfilled: [linkQueryy()] as! [Promise<Any>]).then { _ in
// do more asynch stuff
}
参数fulfilled:
必须是可迭代的.
顺便说一句,when(fulfilled:)
仅在您有很多承诺并且需要等待所有成功完成时才是必需的.但是在您的代码中,您只需要等待一个诺言即可.
By the way, when(fulfilled:)
is necessary only when you have many promises and need wait for all to complete successfully. But in your code, you need to wait for only one promise.
对于单个承诺,更好的方法是形成如下链:
For a single promise, the better way is to form a chain as follows :
firstly {
linkQueryy()
}.then { _ -> Void in
// do more asynch stuff
}.catch { _ in
// error!
}
这篇关于如何在PromiseKit中设置when(fulfilled :)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!