4

I'm using PromiseKit to handle my network calls. I'm trying to see if there's a convention or a cleaner way to either fulfill or reject the promise early. As illustrated below, there are a few conditions that would require me to fulfill or reject early. I'm currently doing this by putting a return statement right afterward. I feel like this is rather clunky and am wondering if there's a better way to do this. Thanks!

return PromiseKit { fulfill, reject in
  if statusCode == 200 {
    if conditionA {
      if conditionB {
        fulfill(...)  // How do I stop the execution chain from here
        return
      } else {
        reject(...) // Or here, without having to call return every time 
        return
      }
    }
    reject(...)
  }
}
7ball
  • 2,183
  • 4
  • 26
  • 61

1 Answers1

8

Rather than using fulfill and reject, you could return the Promise result. Below I have created a function showing you how it can be done:

func someMethod(statusCode: Int, conditionA: Bool, conditionB: Bool) -> Promise<Any> {
    if statusCode == 200 {
        if conditionA {
            if conditionB {
                return Promise(value: "Return value")
            } else {
                return Promise(error: PromiseErrors.conditionBInvalid)
            }
        }
    }
    return Promise(error: PromiseErrors.invalidStatusCode)
}

enum PromiseErrors: Error {
    case invalidStatusCode
    case conditionBInvalid
}

By not using fullfill and reject, you can also clean up the code and move the conditionB check to a new function, such as:

func someMethod(statusCode: Int, conditionA: Bool, conditionB: Bool) -> Promise<Any> {
    if statusCode == 200 {
        if conditionA {
            return conditionASuccess(conditionB: conditionB)
        }
    }
    return Promise(error: PromiseErrors.invalidStatusCode)
}

    func conditionASuccess(conditionB: Bool) -> Promise<Any> {
    if conditionB {
        return Promise(value: "Return value")
    }
    return Promise(error: PromiseErrors.conditionBInvalid)
}

Are you using the PromiseKit extension for Foundation? It helps to simplify networking calls with Promises. You can get the extension here: https://github.com/PromiseKit/Foundation

totiDev
  • 5,189
  • 3
  • 30
  • 30
  • Oh man this is great!! Thanks so much. I've accepted the answer. – 7ball Nov 22 '17 at 08:46
  • A few quick follow-ups if you don't mind: Why should we even bother using "fulfill and reject" like I did in my original question? – 7ball Nov 22 '17 at 08:46
  • 1
    There are a few instances where it makes sense to do so. PromiseKit has an example here 'Wrapping a Delegate Pattern' http://promisekit.org/docs/cookbook/wrapping-delegation/ - here you return a deferred promise that will result in a promise being returned at a later time. In that scenario because you returned a promise, you would need to fulfill or reject it, rather than returning a new Promise instance. – totiDev Nov 22 '17 at 09:11
  • What about using the catch in PromiseKit – MplsRich Apr 27 '19 at 22:38