Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda