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

146
Visualizações
JavaScript run multiple async functions one by one

I have function:

function getLangFile(name, version) {
  var lang;
  lang = "nothing";
  let url = "./" + version + "/" + name + ".json";
  fetch(url)
    .then((response) => response.text())
    .then((data) => {
      setLangToData(data);
    });
}

and

function setLangToData(data) {
   lang = data
}

and i want to run getLangFile("en_gb", "1.18"), and when it get the data, run getLangFile("de_de", "1.18") and then someAnotherFunction()

I know solution is something with callback or promises, but i don't understand it and i wanna learn it later. I have simple beginner project, but it can't work without this. Thanks for any help

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

async function test(){
  const res1 = await getLangFile("en_gb", "1.18");
  const res2 = await getLangFile("de_de", "1.18");
  const res3 = await someAnotherFunction();
}

doc:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try something like this,

const makeHttp = function (url) {
  return new Promise(function (resolve, reject) {
    fetch(url).then((response) => {
      resolve(response.text());
    });
  });
};

(async function () {
  // 1st call
  await makeHttp("./version1/name.json");

  // 2nd call
  await makeHttp("./version2/name.json");
})();
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