[英]How to make sure that all data are pushed to the array before resolving promise?
Here is my code: 这是我的代码:
let promise = new Promise(( resolve, reject ) => {
let dataArray: Array<Object> = [];
d3.csvParse(csvData, ( data, error ) => {
if ( error ) reject('error occurred while parsing csv');
dataArray.push(data);
});
resolve(dataArray);
});
I am using d3 to parse the csv file that is fetched from the server. 我正在使用d3解析从服务器获取的csv文件。 What I am basically trying to do is make sure the parsing is done before returning the promise/object, so I pass it to another function (not shown here).
我基本上想做的是确保在返回诺言/对象之前已完成解析,因此我将其传递给另一个函数(此处未显示)。 Thank you
谢谢
I have read the documentation of csvParse and you can simple change your code with this 我已经阅读了csvParse的文档,您可以使用此命令简单地更改代码
dataArray=d3.csvParse(csvData);
resolve(dataArray);
You don't need to add every row to array into the function because the csvParse will return an array when it finishes to parse 您不需要将数组的每一行都添加到函数中,因为csvParse完成解析后会返回一个数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.