Trying to get a function to output the result of a variable once all of it asynch processes have been completed. To do this I've learned that I have to use promises and so I've spent the day learning about them.
I've written my function with promises and have looked at many tutorials but I am still getting this error, not sure what I am doing wrong. It probably has to do with what I am doing with kpiDefault or how I wrote my kpiAverage function. Also I am using coffee script so it might even be a syntax issue.
here is my code for kpiAverage
  kpiAverage = (period, kpiName, params) ->
    result = $q.defer()
    Sads.shops.getList(params).then (data) ->
      shops = data.map((d) ->
        new ScopeShopWithMetrics(d, $scope.organizations.current)
      )
      $q.all(shops.map((d) ->
        d.getAverages period
      )).then( ->
        shopSum = 0
        i = shops.length
        shopSum += shops[i]["metrics"][kpiName]["value"]  while i--
        shopAverage = shopSum / shops.length)
      .then((shopAverage) ->
          result.resolve shopAverage
          result.promise
        )
Now here is the code that produces the error
kpiDefault = kpiAverage(period7, "visits", testParams).then((shopAverage) ->
   shopAverage
  )
If i do this I don't get an error but the output isn't a number, it looks like it is a promise object.
kpiDefault = kpiAverage period7, "visits", testParams
output
Object {then: function, catch: function, finally: function}
Edit:
So it looks like I'm using promises all wrong but this leaves me even more confused. I simply want the value to be returned after the asynchronous process is done, but now I am more lost than ever.
Looked through code and found out why it was giving me that error (old code that was uncommented by accident) but I am still receiving the promise Object as an output
 
    