Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Promise then not working after AJAX

Tags:

People also ask

Does jQuery return ajax promise?

JQuery does all that and calls success callback if call succeed or error callback if it fails. Jquery also support promise out of box. $. ajax returns a promise object, so that we don't have to pass a callback function.

What is jqXHR in ajax?

The jqXHR Object. The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.

What is promises ajax?

A Promise is an interface for asynchronous operations. An ajax request is a very specific asynchronous operation.


I have my Promise defined as so:

myFunc = function() {
    $.getJSON("./rest/api/some/url", function(json, textStatus) {
        console.log("AJAX call hit!");
    });
};


$.when(myFunc()).then(function() {
  console.log("Then block hit!");
});

and in console it's being output as:

Then block hit!
AJAX call hit!

I need the AJAX call hit! first followed by the Then block hit!.

Any idea why this is happening? I even tried to implement a custom callback function (a standard example I found on Stackoverflow) and it still doesn't work.