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

87
Views
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 answers
Answer question

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 Report
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!