Reputation: 297
I have a function called func1
that's calling var dfd = $.ajax
, and returning dfd.promise()
, now in my main function, I have the code like this:
function addOperation() {
var addPromise = func1();
addPromise.then(
function(sender, args, msg) {
alert("success");
},
function() {
alert("fail");
});
}
The above code is not working, what's working for me is:
function addOperation() {
var addPromise = func1();
addPromise.fail(
function(sender, args, msg) {
alert("fail");
})
.done(
function() {
alert("success");
});
}
From my understanding is that then(functionSuccess, functionFail) is the same as calling .done and .fail on the original promise, isn't that right? I am lost with this.
Upvotes: 0
Views: 52
Reputation: 136074
you should not be returning dfd.promise()
from your function, the return value from $.ajax
is the promise.
Just return dfd
directly (or the result of $.ajax
)
The docs say it best:
The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information). These methods take one or more function arguments that are called when the $.ajax() request terminates
Upvotes: 1
Reputation: 1934
The jqXHR object returned by $.ajax
implements the Promise interface.
The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information).
Therefore, func1 should just return the value returned by $.ajax
.
Upvotes: 0