Fetching asynchronous data
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:2
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had the same issue, I was using the service like this:
<button ng-csv="getArray" filename="file.csv">export</button>
and thegetArray
is a function that returns a promise which is resolved with the data. actually I have solved this issue by setting theng-csv
directive to call the functiongetArray()
so thebutton
will look like this:<button ng-csv="getArray()" filename="file.csv">export</button>
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:
I updated my own application to use that pattern, and it works perfectly.