6

I have a service to for API call as following,

    getValue: function(input) {
        var deferred, url;
        deferred = $q.defer();
        url = "url";
        $http.post(url, input).success(function(data, status, headers, config) {
          return deferred.resolve({
            success: true,
            data: data,
            status: status,
            headers: headers,
            config: config
          });
        }).error(function(data, status, headers, config) {
          return deferred.resolve({
            success: false,
            data: data,
            status: status,
            headers: headers,
            config: config
          });
        });
        return deferred.promise;
      }

But this is async. How can I convert it to sync(I want to make it wait till I get the result)?

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91
Erma Isabel
  • 555
  • 2
  • 11
  • 25
  • 1
    This is what promises are for. Performe your action using the promise you return from the function. e.g the `deffered.promise` you return `.then( function () { /* your action */ })`. Or, in your case (since it basically resolves to the http response, performe your action in the post fn). – haki Oct 28 '14 at 08:15
  • 1
    I think it not possible to do it sync, BUT you using promise which allows you to have control of it, and you can tell your app wait until it will resolve/reject – Narek Mamikonyan Oct 28 '14 at 08:21
  • Possible duplicate of [How to $http Synchronous call with AngularJS](http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs) – Andre Figueiredo Oct 02 '15 at 14:15

1 Answers1

6

No that is not possible with Angular.

See https://github.com/angular/angular.js/blob/master/src/ng/httpBackend.js#L51 where the XMLHttpRequest is opened with

xhr.open(method, url, true);

The third parameter in an xhr.open() can be set to false or true, where false is synchronous and true is asynchronous. In the Angular case, it is hardcoded to true, so that all outgoing calls will be asynchronous.

Use the .success() callback to wait until the async call returns, and then do whatever you want to do there.

As per the suggestion in the comments, you can of course also do the calls via raw javascript, jQuery or any other library that supports synchronous calls, but I would advise using callbacks/defers with the asynchronous angular call, because synchronous calls are blocking and blocking is bad.

Chris
  • 748
  • 2
  • 8
  • 23
Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
  • I was using data from response of $http.get request in an angular service and I had a hard time doing that because the response took some time and due to async nature my code used to call the code that called the service. I didn't use any blocking but I passed data in my application.html and picked it from there before the service gets invoked. Don't know if its the right way to do it but that seems to work. – Ashish Singh Dec 31 '14 at 07:38