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:
- Do I need the dispatch_async in the promise call?
- How do I go about calling and returning nested promises like this?
Thanks
Edit: Correct some of the pseudo-code