gpt4 book ai didi

swift - PromiseKit 首先围绕代码,而不是函数调用

转载 作者:可可西里 更新时间:2023-11-01 00:54:34 24 4
gpt4 key购买 nike

我不想编写一个单独的函数来在我的第一次调用中返回一个 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 在后台队列上运行并异步调用方法(尽管是按顺序)。

我无法弄清楚 PromisefirSTLy 的完整类型规范应该是什么。

最佳答案

据我了解,由于您使用的是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com