2

I'm having an issue with my returned data for the chained then being the 1st deferred data. The first example works:

api.getData().done(function(data){
  api.getData2().done(
    function(data2){
      $.log('success', data2);
    });
  });

But the second example SHOULD work, having data2 for the second .then(), but for some reason its the same as data1.

api.getData().then(function(data1){
  return api.getData2();
}).then(
function(data2){
  $.log('success', data2);
});

Any suggestions?

1
  • 1
    Can you post the code for api.getData2? If I remember correctly, api.getData2 has to return a promise object for this to work correctly, and it also has to resolve the deferred object at some point. I've had this problem before myself. Commented Nov 20, 2012 at 16:48

2 Answers 2

1

$.when will accept two asynchronous functions that returns a promise and execute the .then() function when both are done :

$.when( api.getData(), api.getData2() ).done(function(data, data2) {
    $.log('success', data2);
});

If for some reason (like needing the data) you need to perform getData() before getData2(), there really is no need for .then(), as your first example seems valid enough for that?

Sign up to request clarification or add additional context in comments.

Comments

1

So After some research JQuery Deferred.Pipe I found out I can't chain .then, but instead need to use pipe like below (.pipe() is chainable):

api.getData().pipe(function(data1){
  return api.getData2();
}).then(
function(data2){
  $.log('success', data2);
});

1 Comment

additionally you can finish your chain with a .then (equivalent to both .done and .fail)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.