I have a piece of javascript code where I retrieve a list of employees via an ajax-call.
var employees_json = '';
Ajax.callbackAsync('getEmployees', ['2627016'],
function(response){
employees_json = response;
console.log('this: '+ employees_json);
},
function(error){
alert('Error while retrieving employees: '+ error);
}
);
console.log('that: '+ employees_json);
(2627016 is the branch_id)
the output on the console:
this: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
that:
Why is employees_json empty outside the ajax-call?
Or have I made a type I don’t see!?!
your console.log is executed before the function(response) .
log is not like this ? :
that:
this: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
javascript is 1 thread , yet you never know when the callback function will run.
so you have to adjust your code .