PromiseKit 首先围绕代码,而不是函数调用
PromiseKit firstly around code, not function call
我不想在第一次调用时为 return 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: Ambiguous reference to member 'firstly(execute:)'
None 的示例代码或文档似乎涵盖了这个(我认为是一个)基本用例。在上面的代码中,CSVImporter 在后台队列上运行并异步调用方法(尽管按顺序)。
我无法弄清楚 Promise
或 firstly
的完整类型规范应该是什么。
根据我的理解,由于您在承诺链中使用 then
,它也意味着 return 承诺,因此您会收到此错误。如果您不打算 return 从下一步开始承诺,您可以在 first.
之后直接使用 done
如果你想 return 来自 then
的承诺,请使用下面的链
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)
}
如果您不想 return 来自 then
的 Promise,您可以使用如下所示的链。
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
希望对您有所帮助。
更新:
如果您不想return任何事情并且then
要求return一个Promise
,您可以returnPromise<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)
}
我不想在第一次调用时为 return 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: Ambiguous reference to member 'firstly(execute:)'
None 的示例代码或文档似乎涵盖了这个(我认为是一个)基本用例。在上面的代码中,CSVImporter 在后台队列上运行并异步调用方法(尽管按顺序)。
我无法弄清楚 Promise
或 firstly
的完整类型规范应该是什么。
根据我的理解,由于您在承诺链中使用 then
,它也意味着 return 承诺,因此您会收到此错误。如果您不打算 return 从下一步开始承诺,您可以在 first.
done
如果你想 return 来自 then
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)
}
如果您不想 return 来自 then
的 Promise,您可以使用如下所示的链。
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
希望对您有所帮助。
更新:
如果您不想return任何事情并且then
要求return一个Promise
,您可以returnPromise<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)
}