Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9100823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:57:47+00:00 2026-06-17T00:57:47+00:00

There is related question about processing $http in a service, but I wanted to

  • 0

There is related question about processing $http in a service, but I wanted to elaborate slightly. I would like my controller to be able to execute service calls using API similar to the Angular $http API:

$scope.login = function(user) {
  securityService.login(user).success(function(data) {
    $scope.data = data;
  }).error(function(data) {
    $scope.error = data;
  });
};

That’s a nice readable API. On the surface, all I would need to do in the service API is:

return {
  name : 'User Service',
  login : function(user) {
    return $http.post("/api/login", user);
  }
};

Great, it returns the promise and the success and error messages come with. But… What if I want to deal with the success and failure cases in the service? I want to maintain the nice, readable service API. In this case, maybe I’d want to preserve the user so that I could expose methods like securityService.currentUser() or `securityService.isLoggedIn()’.

I tried the $http.post().then(...) promise API, but those return the entire HTTP response. Again, I want to isolate HTTP to the services and maintain a similar concrete callback API.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:57:48+00:00Added an answer on June 17, 2026 at 12:57 am

    You could make your own promise in login using angular’s $q:

    var deferred = $q.defer(), 
        promise = deferred.promise;
    
    $http.post("/api/login", user).success(...).error(...)
    
    return promise;
    

    Within the success/failure of the $http promise in your service, resolve/reject your deferred object.

    Edit:

    Expanding upon the $http.post:

    $http.post("/api/login", user).success(function(data) {
      if (data == "foo") { // success case?
        deferred.resolve('you logged in!');
      } else {
        deferred.reject('something really bad happened!');
      }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is another possibly related question on this but it didn't have answers and
I've been using the search function but I think there isn't any related question
I saw this related question about publishing toolchain but I know many people did
A related question is here , but my question is different. But, I'd like
I found, that there is related question, about how to find if at least
This question is related with one of my earlier questions.. Previous Post In there
This question is related to a previous post . Is there something comparable to
I want to expand on the functionality related to this question . Is there
Related to an earlier question I asked, I've seen that there's both LoadComponent() and
This is related to another open question of mine . While there aren't any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.