恢复块PromiseKit的转换无效

时间:2018-06-25 12:42:55

标签: swift generics 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()
    }

给出编译时错误

  

类型'(错误)的throwing函数的无效转换throws->   供应商'到非抛出函数类型'(错误)->   保证”

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

  

public func recovery(on:DispatchQueue?=默认,策略:   PromiseKit.CatchPolicy =默认值,_正文:@转义(错误)抛出->   U)-> PromiseKit.Promise,其中U:thenable,Self.T == U.T

来自PromiseKit

我添加了显式类型,并将promise分为多个块,以免遗漏某些东西

1 个答案:

答案 0 :(得分:1)

您应该像这样将Promise正确地链接起来:

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