I have a users service. I would like to create a method which utilizes another service I have built. There are two methods on this service. getUser()
and getCurrentUser()
. getCurrentUser()
utilizes the injected service which acquires the UID. It uses the returned UID to run the getUser()
method. My problem is that I can't get getCurrentUser()
to return the second tier promise.
This question is a little bit difficult to explain. Here's the code...
svs.service("usersService", function($rootScope, $http, loginService, alertBoxService) {
var self = this;
self.getUser = function(identifier, column) {
if (typeof(column) === 'undefined') column = "uid";
return $http.get($rootScope.api + "/getUser/" + identifier + "/" + column);
}
self.getCurrentUser = function() {
var currentUserPromise;
loginService.getCurrentUid().then(
function(response) {
if (response.data === "false") {
alertBoxService.trigger($rootScope.unexpectedApiResponse);
} else {
console.log(self.getUser(response.data));
currentUserPromise = self.getUser(response.data);
}
}, function(response) {
alertBoxService.trigger($rootScope.asyncFailed);
}
);
return currentUserPromise;
}
});
I have a users service. I would like to create a method which utilizes another service I have built. There are two methods on this service. getUser()
and getCurrentUser()
. getCurrentUser()
utilizes the injected service which acquires the UID. It uses the returned UID to run the getUser()
method. My problem is that I can't get getCurrentUser()
to return the second tier promise.
This question is a little bit difficult to explain. Here's the code...
svs.service("usersService", function($rootScope, $http, loginService, alertBoxService) {
var self = this;
self.getUser = function(identifier, column) {
if (typeof(column) === 'undefined') column = "uid";
return $http.get($rootScope.api + "/getUser/" + identifier + "/" + column);
}
self.getCurrentUser = function() {
var currentUserPromise;
loginService.getCurrentUid().then(
function(response) {
if (response.data === "false") {
alertBoxService.trigger($rootScope.unexpectedApiResponse);
} else {
console.log(self.getUser(response.data));
currentUserPromise = self.getUser(response.data);
}
}, function(response) {
alertBoxService.trigger($rootScope.asyncFailed);
}
);
return currentUserPromise;
}
});
Share
Improve this question
asked Aug 31, 2016 at 5:59
AllenphAllenph
2,0152 gold badges29 silver badges47 bronze badges
1
- Yes, of course. But you don't need that variable at all! – Bergi Commented Aug 31, 2016 at 6:09
3 Answers
Reset to default 4Chain the promises but don't forget to chain the rejections too...
self.getCurrentUser = function() {
return loginService.getCurrentUid().then(function(response) {
if (response.data === 'false') {
alertBoxService.trigger($rootScope.unexpectedApiResponse);
return $q.reject(response); // convert into a rejection
}
return self.getUser(response.data); // chain the promise resolution
}, function(response) {
alertBoxService.trigger($rootScope.asyncFailed);
return $q.reject(response); // chain the rejections
});
}
You'll need to inject the $q
service of course.
As Bergi kindly pointed out, you can also reject the promise by throwing an error instead of using $q.reject
, eg
throw response;
The problem here is that when the function getCurrentUser
gets called, it immediately return an undefined
variable. This is because the service is asynchronous and by the time the response is received on the service, the function call would have already returned an undefined
variable. In order to prevent this you can add the return
call inside the loginService
.
For a async function to return a promise from inside the success callback you have to return from the callback which is the promise object and then return from the main function in order to provide the promise to the caller.
Try this.
svs.service("usersService", function($rootScope, $http, loginService, alertBoxService) {
var self = this;
self.getUser = function(identifier, column) {
if (typeof(column) === 'undefined') column = "uid";
return $http.get($rootScope.api + "/getUser/" + identifier + "/" + column);
}
self.getCurrentUser = function() {
return loginService.getCurrentUid().then(
function(response) {
if (response.data === "false") {
alertBoxService.trigger($rootScope.unexpectedApiResponse);
} else {
console.log(self.getUser(response.data));
return self.getUser(response.data);
}
}, function(response) {
alertBoxService.trigger($rootScope.asyncFailed);
}
);
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744732225a4590534.html
评论列表(0条)