0

I would like to use this method from PromiseKit but dont know how to write 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{
    }
 ...

How can I invoke this? Code is from: https://github.com/mxcl/PromiseKit/blob/master/Sources/firstly.swift

Marlen
  • 85
  • 2
  • 8

1 Answers1

0

A basic example of PromiseKit "firstly" usage:

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)
}
kd02
  • 422
  • 5
  • 14
  • Unfortunetley it is not this because it invokes method which wants to use Guranatee. I need to use method which uses Thenable – Marlen Mar 01 '18 at 11:51
  • Can you provide a code sample with what you are trying to do? – kd02 Mar 01 '18 at 11:57
  • @Marlen Assuming your "someMethodWhichReturnsPromise()" is valid the above should work. Maybe take remove your "return" as well. – kd02 Mar 01 '18 at 13:55
  • 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 – Devesh.452 Apr 22 '19 at 19:13