1

我有以下操作:

class CreateObjectOperation {
    // ...

    func promise() -> Promise<Object>
}

class DeleteObjectOperation {
    // ...

    func promise() -> Promise<Void>
}

我希望能够then/catch/finally为两者使用相同的块。我尝试了以下方法:

let foo: Bool = ...
firstly {
    (foo ?
        CreateObjectOperation().promise() :
        DeleteObjectOperation().promise()
    ) as Promise<AnyObject>
}.then { _ -> Void in
    // ...
}.finally {
    // ...
}.catch { error in
    // ...
}

但这不会编译。是否有任何可行的解决方案(除了将代码从块移动到单独的函数,我想避免)?

4

1 回答 1

0

找到了:

firstly { _ -> Promise<Void>
    (foo ?
        CreateObjectOperation().promise().then { _ in Promise<Void>() } :
        DeleteObjectOperation().promise()
    ) as Promise<AnyObject>
}.then { _ -> Void in
    // ...
}
于 2015-08-13T12:42:11.433 回答