0

I have created an application in angularjs promise in which i am calling a method within which a server call will be triggered, i have put a promise enclosing the server call, the server call execution will take some amount of time, after completing it will return the deferred object, everything is fine, but the promise is returning before the server call gets completed

I have mock up a similar scenario within a JSFiddle

function getServerCall() {
    var deferred = $q.defer();
    setInterval(function () {
            $scope.$apply(function () {
                deferred.resolve({
                    "success": true
                });
            });
    }, 3000);

    return deferred.promise;
}

console.log(JSON.stringify(getServerCall().success));
if (getServerCall().success)
{
    console.log('Success');
} 
else{
   console.log('Failed');
} 
Alex Man
  • 4,746
  • 17
  • 93
  • 178

1 Answers1

2

Promise is always returned synchronously. What you want (I think) is to handle the resolved promise.

So, you need to use .then (which is a method available on the promise):

getServerCall()
  .then(function(result){
     if (result.success) { 
        console.log("success");
     } else {
        console.log("failed")
     }
  });
New Dev
  • 48,427
  • 12
  • 87
  • 129
  • still its not synchronous right...check this [JSFiddle](http://jsfiddle.net/jx2sqzgs/1/) `Second Execution` is printing first, how can we lock the whole process until that call get executed – Alex Man Dec 12 '14 at 07:19
  • Javascript is single-threaded ([edge cases aside](http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded)). So, `.then` registers a handler and returns synchronously. Then your `console.log("Second Execution")` is called. You can't stop the execution (if you could, it would freeze your browser) – New Dev Dec 12 '14 at 07:33
  • see in jquery-ajax we have a feature [async](http://stackoverflow.com/questions/20209097/what-is-the-difference-between-asyncfalse-and-asynctrue-in-jquery-ajax/20209122#20209122) for locking everything until it the process get executed successfully, so do we have anything like that in angular – Alex Man Dec 12 '14 at 07:38
  • that wat i want...how to freeze everything including the browser through angularjs like we used to do in jquery-ajax – Alex Man Dec 12 '14 at 07:38
  • jQuery-ajax uses the async parameter of [`open` method of the native `XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Method_overview). Angular's `$http` doesn't support async:false calls at the moment. I'm not aware of any other native way to stop javascript execution outside of `XMLHttpRequest`. – New Dev Dec 12 '14 at 07:45
  • AFAIK, this _only_ works with an Ajax call - not just with any function (like `setInterval`, in your example) And the Angular's version of Ajax (i.e. [`$http`](https://docs.angularjs.org/api/ng/service/$http)) doesn't support blocking. You can always use jQuery-ajax. – New Dev Dec 12 '14 at 07:49