0
I created a function, but I don’t know why when I call this function the return of it comes as Undefined.
follows the code
function serviceDetails(id){
//Service of the wall of cards (Grid Home page).
var _PATH_SERVICE = _app.parseBaseUrl('{{httpsBaseUrl}}relay/moment/'+id);
//MOCK
// var _PATH_SERVICE = '/wall.json';
//AJAX GET service.
_utils.Ajax.get(_app.parseBaseUrl(_PATH_SERVICE))
.done(function(result) {
//console.log(result);
//console.log(result);
//details = result;
return result;
})
.fail(function() {
console.log('Fail in service of wall.');
});
}
The problem seems to be that it is an ajax request. When you call the function, it starts the request and continues running the function, as it has none
return
, it returns Undefined, while ajax return is within the function scope.– Felipe Avelar
The strange thing, that when I put a console.log(result) inside the function, it returns right to requisicao, but when I call this function elsewhere it returns Undefined, Voce knows what can be?
– Bruno Bafilli
As I said, the return value is right, but it gets "stuck" in the scope of the function
serviceDetail
, because he is executed asynchronously. After you start the ajax request, it keeps running the function and, as there is no return in its scope, it returnsundefined
, what you should do is throw that value "out" of the done, assigning it to a global variable, for example.– Felipe Avelar
So, should I create a variable outside the scope to assign the value to it and use? not to call the direct function?
– Bruno Bafilli
That would be one way to solve the problem.
– Felipe Avelar
I had done it, decided, but not sure for what I have to do, I need to run the function right in the event of the click of another function, when I do so there is a delay in the response, it does not bring the return of function in the first click
– Bruno Bafilli
I didn’t quite understand the problem (delay), will always have a delay due to ajax request, no?
– Felipe Avelar