Reputation: 909
I have promise as following code. After I have resolved promise with parameter, I want to get parameter in done() method.
function splitAndSetTebiskopUrl(tebiskopUrl){
splits = tebiskopUrl.split("\u0007");
tebiskopUrl = splits[0];
var defer = $.Deferred();
if(splits[1]){
var cookieValue = splits[1];
return $.ajax({
type:'POST',
url:'xxxxxxxxxxxx',
data:{'cookieValue':cookieValue},
}).then(function(data){
if(data && data.cookieValue){
defer.resolve(tebiskopUrl);
}else{
defer.reject("cookie value doesn't exist");
}
},
function(){
defer.reject("tebiskopCookieSet request error");
});
}else{
defer.resolve(tebiskopUrl);
}
return defer.promise();
}
The parameter is not passed to done() method. Unfortunetly I get it as undefined. I have looked a lot of example. But I can't find the problem.
splitAndSetTebiskopUrl(url).done(function(parameter){
//parameter is undefined
});
Upvotes: 0
Views: 40
Reputation: 664307
In the if
block, you are return
ing the promise from the chained then
call - the promise of the defer
that you resolve will not be returned here. You could just omit that return
keyword and the code would work, but really you should avoid the deferred antipattern and indeed simply return the promise from the chain:
function splitAndSetTebiskopUrl(tebiskopUrl){
var splits = tebiskopUrl.split("\u0007");
tebiskopUrl = splits[0];
if (splits[1]) {
return $.ajax({
type:'POST',
url:'xxxxxxxxxxxx',
data:{'cookieValue': split[1]},
}).then(function(data){
if (data && data.cookieValue) {
return tebiskopUrl;
} else {
throw new Error("cookie value doesn't exist");
}
}, function(err) {
throw new Error("tebiskopCookieSet request error");
});
} else {
return $.when(tebiskopUrl);
}
}
Upvotes: 1