I am building a function that has multiple functions running within it but I want the output to be the value of a single variable. When using coffee script there is no way for me to specify which value I want returned and so it returns multiple functions.
It is very possible that the way I'm writing the code is wrong so feel free to give suggestions.
when i do a console log of the function i get an output like this
Object {then: function, catch: function, finally: function}
The only thing I want returned is the value of ShopAverages, so I expect a number.
here is the function in coffee script
KPIaverage = (period, KPIName, params) ->
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
here is the converted JS output i get when using http://js2coffee.thomaskalka.de/
var KPIaverage;
KPIaverage = function(period, KPIName, params) {
return Sads.shops.getList(params).then(function(data) {
var shops;
shops = data.map(function(d) {
return new ScopeShopWithMetrics(d, $scope.organizations.current);
});
return $q.all(shops.map(function(d) {
return d.getAverages(period);
})).then(function() {
var ShopAverage, i, shopSUM;
shopSUM = 0;
i = shops.length;
while (i--) {
shopSUM += shops[i]["metrics"][KPIName]["value"];
}
return ShopAverage = shopSUM / shops.length;
});
});
};
using angularjs if that makes a difference