I'm trying extract dictionary data like {Name:Value, Name2:Value2,...} from csv file like below
<metadata>
<metadata>
<metadata>
<metadata>
ID,1,2,3,4,5,6,...
Name,ABC,BCD,CDE,DEF,EFG,FGH,...
Val,123,234,345,456,567,678,...
using the function below
async function getMap(file) {
rv = {};
fs.createReadStream(file)
.pipe(csv.parse({from_line: 5, to_line:7, columns: true, bom: true}, (err, data) => {
for(i=1;i<=200;i++) {
rv[data[0][i]] = data[1][i];
}
}));
return rv;
}
I thought this would return something like below:
{'ABC':123,'BCD':234,...}
But empty value was return. I'm sure it's something to do with scope (if that's the right word), but couldn't figure it out yet.
==============================
Initially I thought this is something to do wit scope, but more I looked into the result, it is more of asynchronous issue which I don't understand even more. How could this wait for readStream to process till the end of file, using await, promise, resolve, etc.