2015-08-13 23 views
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 
    // ... 
} 

但是這不能編譯。是否有任何工作解決方案(除了將塊從代碼移動到單獨的函數,我想避免)?

回答

0

發現:

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