我不想编写一个单独的函数来在第一次调用中返回 Promise。我只想写这个:
firstly
{
return Promise<Bool>
{ inSeal in
var isOrderHistory = false
let importTester = CSVImporter<String>(url: url)
importTester?.startImportingRecords(structure:
{ (inFieldNames) in
if inFieldNames[2] == "Payment Instrument Type"
{
isOrderHistory = true
}
}, recordMapper: { (inRecords) -> String in
return "" // Don't care
}).onFinish
{ (inItems) in
inSeal.resolve(isOrderHistory)
}
}
}
.then
{ inIsOrderHistory in
if inIsOrderHistory -> Void
{
}
else
{
...
但我发现有些不对劲。 ImportMainWindowController.swift:51:5:对成员“firSTLy(execute:)”的引用不明确
示例代码或文档似乎都没有涵盖这个(我认为是)基本用例。在上面的代码中,CSVImporter 在后台队列上运行并异步调用方法(尽管按顺序)。
我无法弄清楚 Promise
或 firSTLy
的完整类型规范应该是什么。
最佳答案
据我了解,由于您使用的是 then
在 promise 链中,它也意味着返回一个 promise ,因此您会收到此错误。如果你不打算从下一步返回promise,你可以直接使用done
首先之后。
如果你想从then
返回Promise,请使用下面的链
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.then { (response) in
Promise<Bool> { seal in
print(response)
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
如果你不想从then
返回Promise ,你可以像下面这样使用链。
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
希望对您有所帮助。
更新:
如果您不想退回任何东西并且 then
要求返回 Promise
,您可以返回Promise<Void>
像下面这样。
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.then { (response) -> Promise<Void> in
print(response)
return Promise()
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
关于swift - PromiseKit 首先围绕代码,而不是函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419511/