1

coming from the JS world I'm having a bit of problem wrapping my head around promise kit flavor of promises, I need a bit of help with the following.

Assume I have a function that returns a promise, say an api call, on some super class I await for that promise, then do some other action (potentially another network call), on that parent call I also have a catch block in order to set some error flags for example, so in the end I have something close to this:

func apiCall() -> Promise<Void> {
  return Promise { seal in
    // some network code at some point:
    seal.fulfill(())
  }
}

// in another class/object

func doApiCall() -> ? { // catch forces to return PMKFinalizer
  return apiCall()
    .done {
      // do something funky here
    }
    .catch {
      print("Could not do first request"
    }
}

now I'm trying to write some unit tests for this functionality, so the response is mocked and I know it will not fail, I just need to await so I can verify the internal state of my class:

// on my test file
doApiCall().done {
  // test my code, but I get an error because I cannot pipe a promise that already has a `.catch`
}

How would one go about solving this problem? I could use finally to chain the PMKFinalizer but that feels wrong

Another tangential question would be, is it possible to re catch the error on a higher level, let's say a UI component so it can hold some temporary error state? as far as I see I did not see a way to achieve this.

Many thanks

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • So you are trying to make some unit test on an API call but you don't kknow how to do it, correct ? – Titouan Apr 26 '20 at 08:34
  • Hmm not quite, the question is not really about the API call, but about promises and how to await a finished promiseKit chain (or how to extend it) – Oscar Franco Apr 26 '20 at 08:50
  • I may have a solution that may help you but it doesn't use Promise to return data after the Api call, do you want me to post it ? – Titouan Apr 26 '20 at 09:39
  • hmm I need the promises, I do not want/need to refactor the api call that is not important – Oscar Franco Apr 26 '20 at 16:12

0 Answers0