AngularJS service with promises

I have an AngularJS service which does the following:

  1. It attempts to read user data from Lawnchair (localStorage abstraction with fallbacks).

  2. If user data is not in Lawnchair, the service calls a REST API to retrieve it, and stores the value in Lawnchair.

  3. It returns the user data.

Since both the Lawnchair operation and the REST call are async, I figured this would be a perfect use of Promises.

However, I just can't seem to get a value back. If I debug, I can see values returned from the REST call, and also stored in Lawnchair. If I refresh, I can see the REST call is never made and Lawnchair returns its version.

Any tips are appreciated!

Here is how I am trying to access the value from within my controller:

$scope.userdata = UserDataStore.Data()

Here is the coffeescript for the associated services:

.factory('UserData', ($resource,$location) ->
    return $resource('http://' + $location.host() + '/Services/userinfo')
  )
.factory('UserDataStore', ($resource, $location, UserData, $q) ->
    GetUserDataFromXhr = () ->
      xhrDeferred = $q.defer()
      UserData.get((userdata) ->
        xhrDeferred.resolve userdata.userInfo
        )
      return xhrDeferred.promise
    GetUserDataFromStore = () ->
      storeDeferred = $q.defer()
      Lawnchair ()->
        @get 'userdata',(result) ->
          storeDeferred.resolve result
      return storeDeferred.promise
    return {
      Clear: () ->
        Lawnchair ()->
          @remove('userdata')
      Data: () ->
        sPromise = GetUserDataFromStore()
        combinedPromise = sPromise.then((result) ->
          if not result
            xPromise =  GetUserDataFromXhr()
            xPromise.then((result) ->
              resultString = JSON.stringify(result)
              userdata = {key:"userdata", value:resultString}
              Lawnchair () ->
                @save userdata
            )
            return xPromise
          else
            return result
        )
        return combinedPromise.promise
      }
  )

I found a couple places where I had to fix things:

  1. In the Data method of the returned object, I was trying to return combinedPromise.promise, which would not work considering combinedPromise was already the inner .promise object, thus the Undefined I kept getting back.

  2. In order to get the value to work with in my controller, I had to do so in the promise callback like this:

    UserDataStore.Data().then((result) ->
         $scope.userdata = result
      )