Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot chain $.ajax() using .then() #12

Closed
pjmtdw opened this issue May 22, 2013 · 5 comments
Closed

Cannot chain $.ajax() using .then() #12

pjmtdw opened this issue May 22, 2013 · 5 comments

Comments

@pjmtdw
Copy link

pjmtdw commented May 22, 2013

I'm using chromium 26 browser in Gentoo Linux.
Suppose we have JavaScript code such as

Deferred.installInto(Zepto); // when using Zepto + simply deferred
var first = function(){
  return $.get("/data1")
}
var second = function(data1){
  console.log(data1)
  return $.get("/data2")
}
var third = function(data2){
  console.log(data2)
}
$.when(first()).then(second).done(third)

and each URL returns JSON as follows:
/data1 returns {"data":"one"}
/data2 returns {"data":"two"}

In jQuery 1.9.1, output of console is

{"data":"one"}
{"data":"two"}

This is the expected behavior at least for me.
However, output of Zepto 1.0.0 + simply-deferred is

{"data":"one"}
Object {state: function, done: function, ...}

Is this a bug? Or am I doing something wrong?
I want to get /data2 after getting /data1, and obtain the result of /data2.
Is there any way to do it in same code for jQuery and Zepto + simply-deferred ?

@levic
Copy link

levic commented May 22, 2013

It looks like the done() argument is being called before the second promise has yet resolved, it is not ajax-specific:

Zepto+simply-deferred: http://jsfiddle.net/69eNF/

false
true
resolving firstPromise
firstPromise resolved with result1
secondPromise resolved with Deferred { ... }
resolving secondPromise

jQuery: http://jsfiddle.net/69eNF/3

false
true
resolving firstPromise
firstPromise resolved with result1
resolving secondPromise
secondPromise resolved with result2

Note that in the simply-deferred version that allDone ("secondPromise resolve with...") is called before the secondPromise has actually resolved

@sudhirj
Copy link
Owner

sudhirj commented Jun 8, 2013

Think this might be a bug... will look into it.

@sudhirj
Copy link
Owner

sudhirj commented Jul 9, 2013

This should be fixed in PR #13 . Can you take a look and confirm?

@levic
Copy link

levic commented Jul 10, 2013

Can't speak to the original poster, but for me the fix_12 branch now works in Firefox 22/mac, Safari 6.05/mac and Chrome 28/mac

@sudhirj
Copy link
Owner

sudhirj commented Jul 10, 2013

Thank you... Will merge it in and release v2.1. 

The problem was that the 'then' method affords no special treatment to callbacks that return promises. Had to go read the jQuery docs again to realize it checks the type of the returned value and sets up resolution for it. 


Sent from Mailbox for iPhone

On Wed, Jul 10, 2013 at 9:26 AM, Levi Cameron notifications@github.com
wrote:

Can't speak to the original poster, but for me the fix_12 branch now works in Firefox 22/mac, Safari 6.05/mac and Chrome 28/mac

Reply to this email directly or view it on GitHub:
#12 (comment)

sudhirj added a commit that referenced this issue Jul 10, 2013
Fixes #12. Checking if filters return a promise and resolving / rejectin...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants