PromiseKit chaining with Alamofire
I've been trying to get Promisekit chaining to work with no success. Debugging promisekit... is challenging :)
I have 5 network requests I want to run in serial, e.g: the second one runs after the first completes.
I am using this pattern:
func A() -> Promise<AnyObject> {
return Promise { fulfill,reject in
// alamofire code that calls either fill or reject
}
}
Clone func A for func B,C,D,E.
I want to do
let promise = A()
promise.then { _ -> Void in
return B()
}.then { _ -> Void in
return C()
}.then { _ -> Void in
return D()
}
What I see is that B and C get called at the same after A completes. I feel like I am missing something horribly obvious. I can get the behavior I want by explicitly chaining all of the promises together, but I thought the whole point was that I could avoid that nesting behavior.
What I see is that B and C get called at the same after A completes.
This shouldn't happen, and well, we have tests that prove it doesn't (for test conditions at least). Can you provide a code sample that shows the behavior you are seeing?
Update: To clarify, what you are expecting is how it should behave, so either there is a severe bug in promise kit, or something else is happening.
Which specific tests? I didn't see any that exercised chaining. On May 18, 2015 4:19 PM, "Max Howell" [email protected] wrote:
What I see is that B and C get called at the same after A completes.
This shouldn't happen, and well, we have tests that prove it doesn't. Can you provide a code sample that shows the behavior you are seeing?
— Reply to this email directly or view it on GitHub https://github.com/mxcl/PromiseKit/issues/193#issuecomment-103198032.
I couldn't find anything that explicitly tests chains longer than two, though I'm sure there is something explicit. But there are definitely times it happens during the category tests, because often they chain 34 or 5 promises. Anyway I wrote a new test that tests 1000 sequential thens:
func testThensAreSequentialForLongTime() {
let ex = expectationWithDescription("")
var promise = dispatch_promise { 0 }
let N = 1000
for x in 1..<N {
promise = promise.then { y -> Promise<Int> in
XCTAssertEqual(x - 1, y)
return dispatch_promise { x }
}
}
promise.then { x -> Void in
XCTAssertEqual(x, N-1)
ex.fulfill()
}
waitForExpectationsWithTimeout(10, handler: nil)
}
It passes. I'll push it. I believe this is testing your situation?
I guess it doesn't prove they are sequential, but I can adjust it. One second.
See the commit I just pushed for the revised test, but I believe this shows the behavior you are expecting.
If possible a code sample would help me assist here.
Actually looking closer something is wrong here:
promise.then { _ -> Void in
return B()
}.then { _ -> Void in
return C()
}.then { _ -> Void in
return D()
}
You're stating that all the closures return Void. Shouldn't they be returning a Promise<T>? If you return void, nothing is chained.
If B, C, etc return promises then it seems like a Swift bug that this compiled.
Will have to test. Knew I was doing something wrong. Weird that isn't a compiler error. On May 18, 2015 4:54 PM, "Max Howell" [email protected] wrote:
Actually looking closer something is wrong here:
promise.then { _ -> Void in return B() }.then { _ -> Void in return C() }.then { _ -> Void in return D() }
You're stating that all the clojures return Void. Shouldn't they be returning a Promise
? If you return void, nothing is chained. — Reply to this email directly or view it on GitHub https://github.com/mxcl/PromiseKit/issues/193#issuecomment-103208338.
Interestingly, this compiles fine, and afaict everything chains together fine too:
firstly {
Promise(0)
}
.then { _ -> Void in
println("1")
}
.then { _ -> Void in
println("2")
}
.then { _ -> Void in
println("3")
}
.then { _ -> Void in
println("4")
}
.then { _ -> Void in
println("5")
}
.then { value -> Void in
println(value)
}
In the last closure, I print the "value" returned from the previous Void-returning closure, and it prints "()".
Well yes, returning Void will still chain.
And in the last then, value has type Void (alt click it to see). This is because the previous then returned Void. println returns Void. One-line closures in swift get an implicit return.
Maybe I'm misunderstanding. I thought B() and C() returned promises, no?
Like the point here is: you have to return the promise in your then if you want the next then to wait on that promise. Your example has your thens returning Void.
promise.then { _ -> Void in
return ()
}.then {
// happens immediately
}
promise.then { _ -> Promise in
return Promise { /*…*/ }
}.then {
// happens when the returned promise resolves
}
Ah yes, sorry. It definitely gives me a compiler error when I try to return a promise when the closure specifies a Void return.
I take your point about needing to return a promise if you want to wait on it, that makes sense to me.
For reference the chaining documentation is here: http://promisekit.org/chaining/
If it can be improved please let me know which bits don't make stuff clear etc. as I really want it to be clear. Thanks!
Edit: the docs haven't been updated for PMK2 yet, so they will have Swift versions soon.
Ok, so I got it to work. Unfortunately my example was too contrived and not really close to what I was actually doing. Here is the code that does the wrong thing. In my opinion this is a compiler bug.
let promise = self.A()
promise.then { onComplete -> () in
return self.B()
}.then { onComplete -> () in
return mgr.C()
}.then { onComplete -> () in
return self.D()
}.then { onComplete -> () in
return self.E()
}.then { onComplete -> () in
return self.F()
}
The closure is defined to return void. Since the swift syntax is still new to me I missed that. However, every of my methods is returning a Promise
This code works fine and acts serially:
let promise = self.A()
promise.then { onComplete -> Promise<AnyObject> in
return self.B()
}.then { onComplete -> Promise<AnyObject> in
return mgr.C()
}.then { onComplete -> Promise<AnyObject> in
return self.D()
}.then { onComplete -> Promise<AnyObject> in
return self.E()
}.then { onComplete -> Promise<AnyObject> in
return self.F()
}
So I guess the only suggestion would be to make an example in swift that shows the correct type signature.
K, cool. What page did you learn from and I'll update it.
I'm compiling a list of Swift gotchas so I'll add it there too.
Well, as the swift docs are a bit sparse now I had been looking at http://promisekit.org/chaining/. A good swift example might help :)
Yeah I totally agree, it's long overdue.