0

I am building an application with AngularJS that communicates with an ASP.NET Web Api Server.

In the angular client i have couple of services that communicate with the server through the $http core service.

The problem is that i can't return actual results from the http request but only a promise.

Lets say that i have a service that returns an array of tickets (some resource of my application) from the server:

myService.getAll = function () {

    var result = $http.get('/api/tickets').then(function (result) {

        if (result.status !== 200)
            return [];

        return result.data;

    }, function (err) {

        return [];

    }); 

};

What can i do if i want to explicitly return the data of the result (or an empty array on failure) from the service and not a promise?

Is there a way to wait for a response in Angular?

Thanks, Arik

13
  • 3
    "What can i do if i want to explicitly return the data of the result (or an empty array on failure) from the service and not a promise?" nothing, what you are asking for isn't possible. Returning the promise is the only option that makes sense. Commented Sep 16, 2015 at 18:30
  • "Is there a way to wait for a response in Angular?" Yes, through promises. Commented Sep 16, 2015 at 18:34
  • 1
    You can however resort to using the xhr object manually, nothing prevents you from doing so, but don't forget to start the digest cycle when it completes. I strongly suggest against it. Commented Sep 16, 2015 at 18:37
  • 1
    Yes, it lacks that ability, which makes it a good library. it's hindering you from doing something that is actively harmful to your application. You can almost consider it a feature. Commented Sep 16, 2015 at 18:40
  • 1
    synchronous requests are depreciated and therefore not supported in Angular. DO NOT use them. Commented Sep 16, 2015 at 18:40

2 Answers 2

4

No, what you are asking for isn't possible with angular.

You can get this functionality if you resort to instead using the XMLHttpRequest object, however, I strongly suggest against it because it will negatively impact your application.

Yes, it might make it easier for you to understand and write your code, however, it will negatively impact your end-users by locking the browser up while it is "waiting" for the synchronous request to finish. The user will be unable to do anything within their browser, including changing to other tabs, typing in inputs, clicking buttons, and even animated gifs wont animate. css animations won't run, etc. To the end user, your application will look broken until the request finishes.

To top all of that off, synchronous ajax requests have been deprecated in modern browsers, so it's likely they'll be gone completely in a couple of years (if not sooner,) meaning you'll either have to update your application to use asynchronous requests later, or just not support browsers that drop support for sync requests.

The best option is to embrace promises.

myService.getAll = function () {

    return $http.get('/api/tickets').then(function (result) {

        if (result.status !== 200)
            return [];

        return result.data;

    }, function (err) {

        return [];

    }); 

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

2 Comments

If you still want to know why you can't simply wait with javascript, this video might help: youtube.com/watch?v=8aGhZQkoFbQ Basically, javascript is single threaded, and it will not continue on to the next callback in the callback queue until the current callstack is done executing. this is why a synchronous request causes so many problems in javascript.
Note that the browser rendering tasks also pass through the callback queue, which is why the sync request also stops animated gifs, css animations, and general page view updates.
-2

There is no way to wait fr the response, but you have to pass data to your controller by implementing callback function, for example you service looks like

    myService.getAll = function (callback) {

    var result = $http.get('/api/tickets').then(function (result) {

        if (result.status !== 200)
            return [];
       if(typeof(callback)=="function")
         {
           callback(result.data)
         }


    }, function (err) {

        return [];

    }); 

};

Comments

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.