我在javascript中很新,并且没有找到我的问题的答案。如何在包含异步函数的循环之后完成某件事
实际上,我使用for循环来获取多个csv文件的内容,并将每个内容添加到全局csvstring中,以便在最后下载总csv文件。但是每个csv的内容都是用异步的d3.csv获得的,所以如果我在d3.csv中更新了我的总csvstring,那么因为csvstring还没有更新,我不能用它来保存之后的总csv。我应该怎么做?
我的代码,这是不工作的,如下:
//here is code to define the queries
csvstring = "";
for (i=0 ; i<queries.length ; ++i) {
d3.csv(queries[i], function(data) {
//here is code to format data as a csvstring and then
csvstring += data_as_csvstring;
}
});
//here is code to download csvstring as a csvfile
我希望我自己清楚,我感谢你提前为您解答!
Mag
可能的解决方案:http://stackoverflow.com/questions/23690356/how-to-load-multiple-csv-files-and-use-them-mixed-with-each-other – Andy 2015-02-06 17:17:41
[队列。 js](https://github.com/mbostock/queue)使得您必须更轻松地检索未知数/许多数据文件。 – 2015-02-06 18:05:04