Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Await only valid in async function, waiting for a document to write

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();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!