3

I'm doing a call to a REST service which returns true or false.

function topLevelClosed($stateParams) {
  var id = $stateParams.id;
  return id ? Traject.topLevelClosed({id: id}).$promise : false;
}

var topLevelClosed = {
  method: 'GET',
  url: trajectURL + ':id/topLevelClosed'
};

topLevelClosed is a $resource method. This works perfectly localhost. The topLevelClosed var is 'false', which is equal to the value returned by the REST call. However when deployed (to Google App Engine), I get the result "wrapped in a promise" as seen in the image below. However when I call the REST-service through the browserwindow, it returns false as it should.

promise result when deployed

promise result on localhost

Why won't this work when deployed?

1 Answer 1

2

The behavior you describe is what i would expect for this code. When your function returns a promise your call would look like this

topLevelClosed($stateParams).then(function(value){
  /* do something with value */
});

if instead topLevelClosed returns a boolean then you can access the value directly without the then(...).

To get consistent behavior i would always return a promise, like this:

function topLevelClosed($stateParams) {
  var deferred = $q.defer();
  if(id){
    Traject.topLevelClosed({id: id}).$promise.then(function(value){
      deferred.resolve(value);
    });
  } else {
    deferred.resolve(false);
  }
  return $q.promise;
}

Well this isn't as pretty as i hoped it would be but you can improve it. I do however recommend that you always use a consistent return type. When you mix booleans and promises you have to analyze the return value's type before continuing.

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

4 Comments

He's injecting $stateParams, so this is most likely a resolve method. In a resolve method it doesn't matter if you're returning a promise or not, if it's a promise the router will resolve it for you so you'll always end up with either the resolved promise or in this case the primitive false
@NexusDuck You may want to elaborate on how a $resource method's result ends up in the ui-router to be automatically resolved. I don't see injection here. The only thing i see is a function with a parameter and i don't know how that function is called.
Well, $stateParams is a ui-router service, typically used in resolve functions
From the code in the question i would guess that the function is called and $stateParams is forwarded as an argument, and because we're all lazy when it comes to naming variables the argument variable has the same name in the function. I don't want to jump to conclusions. We probably need more information from the op. I tend to delete my answers that don't match the question, so there should be no harm done.

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.