How can I return value from enclosing js function?
I’ve tried next example, but it doesn’t work(it returns me:
function executeCommand(data, func){
$.ajax({
type: 'POST',
url: SERVERPATH+'Commands/',
data: data,
success: func,
dataType: 'json'
});
}
function executeServiceOperation(data){
var result = null;
executeCommand(data,
result = (function(data,status){
if( status=='success' ){
return data.result;
}
else return null;
})(data,status)
);
return result;
}
result is null everytime. I think it happen because of status. How can I get status variable? Thanks.
Acording to your code, you must pass a function in the second parameter but you are returning an assignment (which is equivalent “true”). It will fail.
instead, do something like this:
However, it’s still wrong. Anything involving AJAX is anynchronous. Thus you can’t “return” anything from it. What you should do is pass a callback when something “successful” happens.
this code above is written in the traditional callback fashion. you should take a look at Deferred Objects for “sugary” code.