0

I have the following code: (I've tried many ways)

performLogin = (data: LoginRequest) => {
  var deferred = this.qService.defer();
  var response = this.httpService.get(
  myServiceURL + '&' + ObjecttoParams(data))
  .then(function (response) {
    debugger;
    this.loginResponse = JSON.parse(JSON.stringify(response.data));
  });
  debugger;
  deferred.resolve(response);
  debugger;
  return(this.loginResponse);
}

This is my constructor for this service:

  constructor($http: ng.IHttpService, $location: ng.ILocationService, $q: ng.IQService) {
            this.qService = $q;
            this.httpService = $http;
            this.locationService = $location;
            this.loginResponse = new LoginResponse();
            this.serviceURL = staticURL + currentServiceName;

        }

I'm calling to performLogin in my controller, who expect to receive a "LoginResponse" class, but because the .then() is happening later, my "return this.loginResponse) happens before the .then() is even called.

How to I make him wait for the .then() to complete before returning?

1 Answer 1

2

Q gives you the ability to create promises, which are very helpful for handling async operations. But the http service returns a promise, so need to create your own.

The promise is what you want to return from this method which will allow the consumer of this method to handle the result asynchronously.

performLogin = (data: LoginRequest): ng.IPromise<LoginResponse> => {
  var response = this.httpService.get(
      myServiceURL + '&' + ObjecttoParams(data))
    .then(function (response) {
      return response.data;
    });
  return response;
};

Then whatever is calling performLogin can do this:

foo.performLogin(x)
  .then((response) => {
    // do something here with the response data
  });

Bonus Points Saving the extra boilerplate of taking the constructor dependencies and mapping them directly onto the object...

constructor(
    private $http: ng.IHttpService,
    private $location: ng.ILocationService,
    private $q: ng.IQService) {
  // NOT NEEDED this.qService = $q;
  // NOT NEEDED this.httpService = $http;
  // NOT NEEDED this.locationService = $location;
  this.loginResponse = new LoginResponse();
  this.serviceURL = staticURL + currentServiceName;
}

By adding private in front of the params it will automatically add them as properties to the instance. BUT they will be the name they are injected as... so you would have to use this.$http instead of this.httpService

Sign up to request clarification or add additional context in comments.

2 Comments

tnx, trying right now, the Q.Promise should be $q ?
Ah yes, thank you... I have been doing a lot of node, I will update the answer accordingly... it will actually be ng.IPromise as the return type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.