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

91
Visualizações
I can't get fetch to work with async/await

I'm starting with Javascript, I am trying to make a fetch behave synchronously. I need that after calling a function that ends up calling the fecth to process the result.

async function _send_sync_command(command) {
  const response = await fetch("http://localhost:5700" + "/ext", {
  method: "POST",
  headers: {
        "Content-type": "application/json; charset=UTF-8"
    },
  body: JSON.stringify(command)
  });

  const data = await response.json();
    return data;
}

function send_command(command) {
  this._send_sync_command(command)
  .then((data) => {
    return data;
  })
}


function DownloadToFP(star_cmd, next_cmd, end_cmd) {
  var json_out = {};
  json_out.data_0 = "";
  json_out.data_1 = "";
    
  // Start download command
  var response = this.send_command(star_cmd);

  // I need this to run after all promises are resolved !!!

  if (response.fields.length > 0) {
    json_out.data_0 = response.fields[0];
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to await the _send_sync_command function to stop the execution of the function at this point, and return back when all promises are resolved.

Also, I don't think you need send_command function.

UPDATE

as @danh suggests to return json_out from DownloadToFP function.

async function _send_sync_command(command) {
  const response = await fetch("http://localhost:5700" + "/ext", {
  method: "POST",
  headers: {
        "Content-type": "application/json; charset=UTF-8"
    },
  body: JSON.stringify(command)
  });

  const data = await response.json();
    return data;
}


async function DownloadToFP(star_cmd, next_cmd, end_cmd) {
  var json_out = {};
  json_out.data_0 = "";
  json_out.data_1 = "";
    
  // Start download command
  var response = await this._send_sync_command(star_cmd);

  // I need this to run after all promises are resolved !!!

  if (response.fields.length > 0) {
    json_out.data_0 = response.fields[0];
  }
  return json_out
}

about 4 years ago · Juan Pablo Isaza Relatório
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