Note that there are some explanatory texts on larger screens.

plurals
  1. PODeferred object returned before it is rejected or resolved?
    primarykey
    data
    text
    <p>I have a function that basically looks like this:</p> <pre><code>function defTest() { var dfd = new jQuery.Deferred(); $.ajax( { type: "GET", url: 'http://XXXX', cache: false, dataType: "json", success: function(data,status) { console.log('ajax done: success'); dfd.resolve(); }, error: function(data,status) { console.log('ajax done: fail'); dfd.reject(); } }); console.log('about to return dfd'); return dfd; } </code></pre> <p>I call it like this:</p> <pre><code>defTest().then(.....); </code></pre> <p>The console log produce this: about to return dfd code.js:106 ajax done: success code.js:96</p> <p>What confuses me is that the code seems to work. Still the dfd is returned BEFORE the ajax has finished. So i removed the return dfd. And put it last in each ajax handler function to make sure that this will NOT be returned until the Ajax has finished.:</p> <pre><code>success: function(data,status) { console.log('ajax done: success'); dfd.resolve(); return dfd; } </code></pre> <p>Then it didn´t work at all. I am confused! Can someone explain to me why my deferred can´t be in the ajax success and error handlers and why it works even though it seems that my deferred object returns even if it is fired BEFORE the Ajax is finished and then be resolved or rejected? How is that even possible?</p> <p>EDIT: This issue is directly linked to my previous unanswered and more complex function: <a href="https://stackoverflow.com/questions/12365568/problems-with-deferred-object">Problems with deferred object</a></p> <p>This is why i can´t just "return ajax(...)" because my real function contains other ajax calls that will be apart of ONE result handed back to the caller.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload