【问题标题】:PromiseKit 3.0 chainingPromiseKit 3.0 链接
【发布时间】:2016-09-11 00:56:56
【问题描述】:

我正在尝试编写一个返回承诺的函数:

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' 都应该隐含地返回一个承诺;我的想法错了吗?我应该像这样明确地返回一个承诺吗?:

func sample() -> Promise<AnyObject> {
    return Promise(1)
    .then { _ -> Void in
        debugPrint("foo")
    }.then { _ -> Promise<AnyObject> in
        debugPrint("foo")
        return Promise(1)
    }
}

谢谢

【问题讨论】:

    标签: swift promisekit


    【解决方案1】:

    then 调用返回您在其调用中指定的任何内容,在您的第一个示例中为 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 将按顺序运行,以及您添加到函数返回的承诺中的任何其他内容,当它在您的代码中的任何其他位置被调用时。

    【讨论】:

    • 感谢@DaveWood;尽管您的示例仍然存在相同的错误。我什至删除了第一个“then”。这是我现在所拥有的:func sample() -&gt; Promise&lt;AnyObject&gt; { return Promise&lt;AnyObject&gt; { fulfill, reject in return Promise&lt;AnyObject&gt;(1) }.then { _ -&gt; Void in debugPrint("foo") } } 有什么想法吗?
    • 您再次更改了代码,使您的 then 处于外部承诺而不是内部承诺。
    • 你知道为什么在这种情况下不需要调用 'fulfill' 吗? @戴夫伍德
    • 实际上,除非我履行外部承诺,否则我的日志中会出现此错误:Promise deallocated! This is usually a bug@DaveWood
    • 实际上,我会投票支持@Jeffery-Thomas 的答案,因为它是正确的,而不是我的。他无需使用内部承诺或使用完成/拒绝即可生成更简洁的代码。他对最后一个then的返回值必须与方法返回的promise类型相匹配的解释很清楚。
    【解决方案2】:

    then(_:) 返回的 promise 与闭包的返回值匹配。

    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&lt;AnyObject&gt; 的预期返回类型与Promise&lt;Void&gt; 的实际返回类型不匹配。

    如果你想让一个方法返回Promise&lt;AnyObject&gt;,那么promise链中的最后一个promise必须返回AnyObject

    func sample() -> Promise<AnyObject> {
        return firstly { _ -> Void in
            debugPrint("foo")
        }.then { _ -> Void in
            debugPrint("foo")
        }.then { _ -> AnyObject in
            1
        }
    }
    

    【讨论】:

    • 感谢您的出色解释。通过阅读源代码和文档很难理解这一点。在“首先”之后的块内,我在debugPrint("foo") 行上收到了一个奇怪的错误Ambiguous reference to member 'debugPrint(_:separator:terminator:)。但是我将其替换为以下内容:return Promise&lt;AnyObject&gt; { fulfill, reject in var fish = 1 }.then { _ -&gt; Void in debugPrint("foo") }.then { _ -&gt; AnyObject in 1 } 它工作正常@JefferyThomas
    猜你喜欢
    • 2023-03-29
    • 2021-05-12
    • 2015-12-14
    • 1970-01-01
    • 2016-02-08
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    相关资源
    最近更新 更多