question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetching asynchronous data

See original GitHub issue

I’ve gone through the issues and nothing is ever answered in a way that makes this work for me.

Basic example:

var loadData = function() {
   var deferred = $q.defer();
   $http.get('/api/data')
       .then(function (res) {
           deferred.resolve(res.data);
        }, function (res) {
           deferred.reject();
        });
   };
   return deferred.promise;
}

This doesn’t work. The file immediately gets downloaded with 0 data. Can someone please tell me if there is another way to do it for ng-csv? I looked at the lazy-load example but I’m not sure what the purpose of that is.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

7reactions
SamiSammourcommented, Nov 21, 2016

I had the same issue, I was using the service like this: <button ng-csv="getArray" filename="file.csv">export</button> and the getArray is a function that returns a promise which is resolved with the data. actually I have solved this issue by setting the ng-csv directive to call the function getArray() so the button will look like this: <button ng-csv="getArray()" filename="file.csv">export</button>

2reactions
ryan-hunter-pccommented, Jul 19, 2016

I found this, facing the same question in my project, and the $q.defer() pattern in @daniel-van-niekerk 's comment solved it for me. However, during the same search I discovered (thanks to this blog) that promise-chaining can accomplish the same thing much more simply:

var loadData = function () {
    return $http.get('/api/data').then(function (res) {
        return res.data;
    });
};

I updated my own application to use that pattern, and it works perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Fetch with async/await - Dmitri Pavlutin
How to use fetch() with async/await syntax in JavaScript: fetch JSON data, handle errors, make parallel requests, cancel requests.
Read more >
The Evolution of Asynchronous Data Fetching in JavaScript
This post covers techniques for writing asynchronous code in JavaScript with callbacks, promises and async/await—allowing for time-consuming ...
Read more >
How to use the Fetch API with async/await - RapidAPI
Fetch API is an asynchronous web API that comes with native JavaScript, and it returns the data in the form of promises.
Read more >
Fetching Asynchronous Data with React Hooks - Giorgio Polvara
We fetch our resource and put the result in the state. Our render method uses a ternary operator to decide if we want...
Read more >
How to fetch data from APIs using Asynchronous await in ...
In this article, we are going to make an API request to any APIs that you want using ReactJS, and fetch data using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found