【问题标题】:Synthax of firstly in PromiseKit 6PromiseKit 6 中 firstly 的合成器
【发布时间】:2018-03-01 11:36:09
【问题描述】:

我想使用 PromiseKit 中的这个方法,但不知道如何编写propper synthax :x

public func firstly<U: Thenable>(execute body: () throws -> U) -> Promise<U.T> {
do {
    let rp = Promise<U.T>(.pending)
    try body().pipe(to: rp.box.seal)
    return rp
} catch {
    return Promise(error: error)
}

}

 firstly {
           return someMethodWhichReturnsPromise()
        }.then{
    }
 ...

我该如何调用它? 代码来自:https://github.com/mxcl/PromiseKit/blob/master/Sources/firstly.swift

【问题讨论】:

    标签: ios swift promisekit


    【解决方案1】:

    PromiseKit“firstly”用法的基本示例:

    func someMethodWhichReturnsPromise() -> Promise<Int> {
    
        // return promise
    }
    
    firstly {
    
        someMethodWhichReturnsPromise()
    
    }.then {
    
        // Execute more logic here after 'firstly' block completes
    
    }.catch {
    
        // handle error if you need to
        handle(error: $0)
    }
    

    【讨论】:

    • 不幸的是不是这个,因为它调用了想要使用 Guranatee 的方法。我需要使用使用 Thenable 的方法
    • 你能提供一个代码示例来说明你想要做什么吗?
    • @Marlen 假设您的“someMethodWhichReturnsPromise()”是有效的,以上应该可以工作。也许把你的“回报”也去掉。
    • func add(a:Int, b:Int) -> Promise { return Promise { seal in if a > b { seal.fulfill(2) } else { seal.reject(NSError (domain: "aa", code: 43, userInfo: nil)) } } } Like this
    猜你喜欢
    • 2021-05-12
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    相关资源
    最近更新 更多