PromiseKit icon indicating copy to clipboard operation
PromiseKit copied to clipboard

PromiseKit chaining with Alamofire

Open carlshimer opened this issue 11 years ago • 16 comments

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.

carlshimer avatar May 18 '15 19:05 carlshimer

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.

mxcl avatar May 18 '15 20:05 mxcl

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.

carlshimer avatar May 18 '15 20:05 carlshimer

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?

mxcl avatar May 18 '15 20:05 mxcl

I guess it doesn't prove they are sequential, but I can adjust it. One second.

mxcl avatar May 18 '15 20:05 mxcl

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.

mxcl avatar May 18 '15 20:05 mxcl

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.

mxcl avatar May 18 '15 20:05 mxcl

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.

carlshimer avatar May 18 '15 21:05 carlshimer

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 "()".

apendley avatar May 18 '15 21:05 apendley

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?

mxcl avatar May 18 '15 21:05 mxcl

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
}

mxcl avatar May 18 '15 21:05 mxcl

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.

apendley avatar May 18 '15 21:05 apendley

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.

mxcl avatar May 18 '15 22:05 mxcl

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 should have failed for returning the wrong type. However, it compiles cleanly and the return is ignored.

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.

carlshimer avatar May 19 '15 01:05 carlshimer

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.

mxcl avatar May 19 '15 01:05 mxcl

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 :)

carlshimer avatar May 19 '15 01:05 carlshimer

Yeah I totally agree, it's long overdue.

mxcl avatar May 19 '15 04:05 mxcl