wagster
wagster

Reputation: 538

jquery promise not firing

The then function isn't firing after loading this XML file. I'm not getting any errors and I can see the file loading in the network tab. What am I doing wrong here?

$.when(
    $.get("data/data.xml", {}, function (file) {
        xml = file;
    })
).then(function(){ 
    console.log("Loaded");
});

Upvotes: 0

Views: 128

Answers (3)

danwellman
danwellman

Reputation: 9273

In this case, why use $.when at all, the get method returns a promise (or deferred), so you should be able to chain then on that method directly

$.get('data/data.xml', {}, function (file) {
    xml = file;
}).then(function(){ 
    console.log('Loaded');
});

Upvotes: 3

Ravi MCA
Ravi MCA

Reputation: 2631

I suggest to user jQuery recommended call back functions as here.

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

In your case you can use done() or always().

If you want to execute on success.

$.get("data/data.xml", {}, function (file) {
        xml = file;
} ).success( function(){ 
    console.log("Loaded");
} );

If you want to execute on no matter of success or error.

$.get("data/data.xml", {}, function (file) {
        xml = file;
} ).always( function(){ 
    console.log("Loaded");
} );

Upvotes: 0

wagster
wagster

Reputation: 538

In this case it was because the xml was malformed. It looks like if the xml is loaded but can't be parsed, the $.get callback isn't fired but no error is thrown.

Upvotes: -1

Related Questions