-2

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

grasshopper
  • 918
  • 4
  • 13
  • 29
  • 1
    Looks like you're misunderstanding promises, and asynchronous flow. You don't return things, you use callbacks. I suggest you read about continuation passing style (CPS), and how that applies to async code. It will clear up your confusion. – elclanrs Aug 25 '14 at 07:53
  • Note the [deferred anti pattern](http://stackoverflow.com/questions/23803743/what-is-the-deferred-anti-pattern-and-how-do-i-avoid-it) in your code. You can avoid that since promises chain. – Benjamin Gruenbaum Aug 25 '14 at 07:54
  • 1
    Also related: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Benjamin Gruenbaum Aug 25 '14 at 07:55
  • hmmm... I was told before that I need callbacks I thought thats what promises were for, how would I make a callback for this? What exactly is a callback? – grasshopper Aug 25 '14 at 07:59

1 Answers1

0
// Here is my function in my Factory which retrieve DATA.
result.retrieveResult = function(){
var deferred = $q.defer();

return $http.get( constant.adressApi + 'result', { cache : true, params :{ 'limit' : 50 } } )

.success(function (data) { deferred.resolve(data); })
.error(function(error){ deferred.reject(); });

return deferred.promise;
};

    // Here is my call to the function from my factory service.
    presentationService.retrieveResult()
    .then(function(result){
    $scope.result = result.data;
    });

This is how i use Promise easily hope this can help you.

As you can see you create a deferred(some people call it futur). you return it at the end of the function and it will represent your result. As soon as your asynchronous function return the result if everything was fine the .success callback is call and the deferred is resolve in that case you should get the result where i wrote .then( function(result) ... Otherwise the .error is call and then you retrieve the get the reason of the error.

Short Answer I don't see anywhere in your code that you resolve or reject your deferred so i think that's why your output display :

Object {then: function, catch: function, finally: function}
Anddo0
  • 59
  • 1
  • 3
  • I did exactly this I named my variabel result instead of deferred I'll change that now – grasshopper Aug 25 '14 at 08:13
  • .success(function (data) { deferred.resolve(data); }) .error(function(error){ deferred.reject(); }); I don't see the equivalent in your code. What happen with this is that when the success or reject will be reach it will change your Object {then: function, catch: function, finally: function} with the result – Anddo0 Aug 25 '14 at 08:16
  • do you think adding another .then to the kpiAverage function will work then? – grasshopper Aug 25 '14 at 08:25
  • OK my bad you actually resolve it. i am watching it trying to understand i don't know the syntax you use ^^ is that coffee script ? – Anddo0 Aug 25 '14 at 08:29
  • yes this is coffee script you can visit http://js2coffee.thomaskalka.de/ to get a pure js translation – grasshopper Aug 25 '14 at 08:32