I'm learning Electron by writing the app that should parse and analyze .csv files. As a frontend part of it I'm using Vue.js. Initial functionality should be next: user selects a file, main process parses it, file's summary displayed at the client as a part of a Vue component. Code below:
//background.js
import Analyzer from './Analyzer'
analyzer = new Analyzer();
ipcMain.on('data:upload', (event, data) => {
let options = {
filters: [{
extensions: ['csv']
}]
};
let filepath = dialog.showOpenDialog(options);
analyzer.loadDataCSV(filepath); //executes async code
event.sender.send('data:upload', analyzer.getState());// doesn't wait
}) // async code to be completed
// so the 'state' I'm sending
// is actually incorrect
//Analyzer.js - class that i've wrote
import csv from 'fast-csv'
...
loadDataCSV(filepath) {
if (!filepath) return;
this.dataWSFilepath = filepath;
this.showDataCard = true;
let rowsCount = 0;
csv. //async part
fromPath(filepath, { delimiter: "|" })
.on("data", function (data) {
if (data[9] == 'NCR') rowsCount++;
})
.on("end", function () {
this.NCRCount = rowsCount //Variable I'm expecting to be
//changed inside the Vue component
});
}
getState(){ return { NCRCount: this.NCRCount }}
...
Please tell me the right way to wait the async part of loadDataCSV() to be completed before sending answer to ipcRenderer.