恢复块的无效转换,PromiseKit

Invalid conversion of recover block, PromiseKit

此代码

let promise: Promise<Supplier> = self.supplierController
                        .update(supplier: supplier)

let block: ((Error) throws -> Supplier) = { (error: Error) throws -> Supplier in
    let supplier: Supplier = supplier
    guard (error as NSError).code == 405 else {
        throw error
    }
    return supplier
}

let newPromise = 
promise
    .recover(block)
    .done { (_: Supplier) in
        changeCompanyIdAndAppendMessage()
    }

给出编译时错误

invalid conversion from throwing function of type '(Error) throws -> Supplier' to non-throwing function type '(Error) -> Guarantee'

为什么要尝试转换?在我看来,它必须使用这个 func:

public func recover(on: DispatchQueue? = default, policy: PromiseKit.CatchPolicy = default, _ body: @escaping (Error) throws -> U) -> PromiseKit.Promise where U : Thenable, Self.T == U.T

来自 PromiseKit

我添加了显式类型并将 promise 分成块,以免遗漏某些内容

您应该return保证正确链接,如下所示:

self.supplierController
    .update(supplier: supplier)
    .recover { error -> Promise<Supplier> in
        let supplier: Supplier = supplier
        guard (error as NSError).code == 405 else {
            throw error
        }

        return .value(supplier)
    }
    .done { (_: Supplier) in
        changeCompanyIdAndAppendMessage()
    }

来源:https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md