my previous post was closed because it was marked as a duplicate when it wasn't and doesn't answer the question. It was suggested to ask again. The question is how do i return the object value from the jQuery $.post() function call when it's inside another function. the post they reference does not answer this question. The .then() is treated as a promise with jQuery but can't figure out how to get that value to return from the containing function.
ref post: How do I return the response from an asynchronous call?
all of the answers listed wait as expected until the ajax is complete and use a form of callback function like the jQuery .then() but none explain how to return that through a containing function, unless i'm missing it. If i'm missing it I apologize and would appreciate someone pointing out where i'm missing it.
Meisam Mahdian in the OP gave an example of using var dfd = $.Deferred();
and then returning dfd but this does not seem to work, all i get back is the $.post() prototype as does the function i'm currently using.
In other languages I would just pass the variable by reference but that doesn't exist in javascript (that i can see)
The question is how to return a promised value from a jQuery $.post() when it's inside another function. So that
var thisData = getItemData(id, div);
located in another script will set the variablethisData
to the JSON response received from jQuery without resorting to a global variable (IE: sData in the OP).
another example is trying to get the same effect as the following but using the jQuery $.post() function in replace of the innerFunction()
var test = outerFunction();
function outerFunction() {
function innerFunction() { return 'yay'; }
return innerFunction();
}
I have also tried different variations of return, including a local variable and .then() modifying it and returning that. Those didn't work either.
OP:
I'd like to set a variable from a function's return data, however, there is a $.post() call inside. How do i modify the following code to return the data. i've looked at a bunch of suggestions but none seem to work.
Currently the sData inside the .then() is a global variable, i'd like to use that as the return value so a local call anywhere of var thisData = getItemData(id, div);
will be the same as the current global variable sData. I saw using a callback function but this doesn't solve the issue as i'd have to use the global variable inside that callback.
forgive the complex ternary, there are 3 possible JSON types that could be returned server side.
var sData;
function getItemData(id, div) {
typeof id !== 'undefined' ? id : '220';
typeof div !== 'undefined' ? div : '';
beginLoading(div);
return $.post({
url: '/content/itemsjson/?categoryid=' + id,
data: { },
})
.done(function(data, status, request) {
//***
})
.fail(function(xhr, textStatus, errorThrown) {
//***
})
.always(function(data) {
//***
})
.then(function(data) {
stopLoading(div);
sData = (typeof data == 'string') ? JSON.parse(data.responseText) : (data[0] ? data[0] : data);
});
}