Issue
I need to check data availability in controller, that is provided by service. Service is using HTTP request to get some data - depending on response function is returning just true or false. Boolean value is returned to controller and if
is executed on returned value.
But it doesn't work, controller doesn't wait for value, in debugger the variable is "undefined", if
is executed as false
and controller is going to the next code.
As there is HTTP request I am using .then for response handling in service. I tried also to use promise
and .then
in controller. None of them helped.
Here is a code from controller:
var t5 = PermissionService.permissions.gotRequiredPermissions();
if (t5) {
// some processing
}
and here is function from service:
function gotRequiredPermissions() {
ApiService.getUserPermissions().then(function successCallback(response) {
//processing
//returns true or false
}, function errorCallback(response) {
return false;
});
}
Function in controller is called, then function is service is called, then controller skips the if
as t5 is undefined, goes to code after if
and then, when controller finish it's stuff, service process the successCallback
and returns value.
Solution
Modify gotRequiredPermissions()
function to return a promise:
function gotRequiredPermissions() {
return ApiService.getUserPermissions().then(function successCallback(response) {
//processing
//returns true or false
}, function errorCallback(response) {
return false;
});
}
In controller you should get the result:
PermissionService.permissions.gotRequiredPermissions().then(result=> console.log(result))
Answered By - Bill P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.