asafdav / ng-csv

Simple directive that turns arrays and objects into downloadable CSV files
MIT License
573 stars 215 forks source link

More problems with promises #105

Open KevinMitchell opened 8 years ago

KevinMitchell commented 8 years ago

I’m having problems getting promises working with ng-csv

So my HTML looks like this: <button ng-csv="getCSVResultsData()" filename=“results.xslx">Results</button>

If my getCSVResultsData function looks like this:

$scope.getCSVResultsData = function () {
   var deferred = $q.defer();
   deferred.resolve([{'name': 'Fred'}, {'name': 'Fred'}, {'name': 'Fred'}]);
   return deferred.promise
    .then(function(arr) {
      console.log(arr)
      return arr
    })
};

Then it works as expected. However, if I define it by this:

$scope.getCSVResultsData = function () {
  var promises = … // Build an array of promises, each returning a row
  return $q.all(promises)
    .then(function(arr) {
      console.log(arr)
      return arr
    })
};

Then I don’t get a CSV file, even though the data logged in the console is the same. The only difference I can see is that in the first case the promise has the data available immediately, and in the second it is really asynchronous. I presume this should work? If so, any idea why it isn’t for me?