2

I am trying to use PromiseKit, and am having a bit of trouble wrapping my head around this. I have a function that does something like

func lengthyOperation() -> Promise<TestObject> {
   return Promise { fulfil, reject in
     dispatch_async(GlobalUserInitiatedQueue) {
       do {
          let testObject = ...
          fulfil(testObject)
       } catch {
          reject(error)
       }
     } 
   }
}

I have another function that I would like to call this in, and return another promise

func lengthyOperation2() -> Promise<Test2Object> {
   return Promise { fulfil, reject in 
        let test2Object = ...
        if test2Object == nil {
           lengthyOperation().then { testObject: TestObject in
               let test2Object = doSomethingWithTestObject(testObject) 
              fulfil(test2Object) //Compiler error here
           }.error {
              reject(error)
           }
        } else {
           fulfil(test2Object)
        }
   }
}

I get a compiler error saying

Cannot convert return expression of type 'Void' (aka '()') to return type 'AnyPromise'

Couple of questions:

  1. Do I need the dispatch_async in the promise call?
  2. How do I go about calling and returning nested promises like this?

Thanks

Edit: Correct some of the pseudo-code

georgemp
  • 716
  • 10
  • 21

1 Answers1

0

Just in case it helps somebody else,

  1. From the docs, the promise call is made on the same queue as which it is called from. So, if we want code to run in the background (in the first lengthyOperation()), it will need to be wrapped in a dispatch_async or similar.
  2. I seem to have gotten things working by the following pattern

    func lengthyOperation2() -> Promise<Test2Object> {
        return firstly {
           return lengthyOperation()
        }.then { (testObject: TestObject) -> Promise<Test2Object> in
           let test2Object = doSomethingWithTestObject(testObject)
           return Promise(test2Object)
        }
    }
    
georgemp
  • 716
  • 10
  • 21
  • I don't think you will need to use dispatch_async. Maybe `thenOn` or `thenInBackground`. http://promisekit.org/dispatch-queues/ – Jon Jul 12 '16 at 16:56
  • Jon - Thanks. But, unless I am missing it, I don't see a way to create your own promises on a background queue? – georgemp Jul 12 '16 at 22:12
  • Did you read the last three paragraphs in that link? `thenOn` processes off the main thread. `thenInBackground` dispatches onto default GCD queue. – Jon Jul 12 '16 at 22:27
  • Jon - I did go through that link. However, we seem to be talking about different things. http://promisekit.org/sealing-your-own-promises/ talks about creating/sealing your own promises. In the example provided as well, they seem to be executing on a background queue and fulfilling with a completion handler. thenOn and thenInBackground is for continuing a promise chain. Btw, I am referring to func lengthyOperation() when I talk about dispatching in the background. Have edited my answer to make that clearer – georgemp Jul 12 '16 at 22:54