I'm trying to write to a file then send that file. I need to wait for the file to write before sending then wait for it to send before looping again. I have the file creation function and file sending function returning promises and the start function is async. However I'm getting await only vaild in async function.
const fs = require('fs');
const csv = require('csv-parser')
const { exec } = require("child_process")
var i = 74243;
async function start() {
fs.createReadStream('agency_summary_test.csv')
.pipe(csv())
.on('data', function (row) {
var cmd = "curl -XPOST -u \'User:Pass\' ";
var url = "\'https://vpc-test-ovlb6af5uhpnaoqb57oqg5xore.us-gov-east-1.es.amazonaws.com/_bulk\' --data-binary @agency_temp.json";
var obj = {}
Object.entries(row).forEach(([key, value]) => {
if (!isNaN(value)) {
value = parseInt(value)
}
obj[key] = value
})
await writeFile(obj, i);
var cmdString = cmd + url + " -H \'Content-Type: application\/json\'";
await sendFile(cmdString);
i++;
})
}
function writeFile(obj, i) {
return new Promise((resolve) => {
fs.writeFileSync('agency_temp.json', JSON.stringify({
index: {
_index: "agency_summary",
_id: i
}
}) + "\n" + JSON.stringify(obj) + "\n", (err) => {
if (err)
console.log(err);
else { }
})
resolve();
})
}
function sendFile(cmd) {
return new Promise((resolve) => {
exec(cmd, (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`)
});
resolve();
})
}
start();