Estoy comenzando con Javascript, estoy tratando de hacer que una búsqueda se comporte sincrónicamente. Lo necesito después de llamar a una función que termina llamando a la función para procesar el resultado.
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]; } }Debe await la función _send_sync_command para detener la ejecución de la función en este punto y regresar cuando se resuelvan todas las promesas.
Además, no creo que necesites la función send_command .
ACTUALIZAR
como @danh sugiere devolver json_out desde la función DownloadToFP .
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 }