对两个不同类型的承诺使用相同的 then/catch/finally 块

Using same then/catch/finally blocks for two promises of different types

我有以下操作:

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
    // ...
}

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

找到了:

firstly { _ -> Promise<Void>
    (foo ?
        CreateObjectOperation().promise().then { _ in Promise<Void>() } :
        DeleteObjectOperation().promise()
    ) as Promise<AnyObject>
}.then { _ -> Void in
    // ...
}