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

194
Visualizações
Is there a way to make async function sync?

I have code like this.

async function doAsync(count){
  //external function I need to use
  count++;
  console.log("async count is "+ count );
  return await count;
}
 
function makeSyncChain(i){
  //my chain that I could change
  i=doAsync(i);
  return i;
}

let val=0;
console.log("sync count is " + makeSyncChain(val));
so like at the example, I have sync chain and one of the chain function is async, is there a way to make async function working in sync chain?

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

0

Correct solution must be:

async function doAsync(count){
  //external function I need to use
  count++;
  console.log("async count is "+ count );
  return await count;
}

let val=0;
console.log("sync count is " + (await doAsync(val)));

But in wrong key may be written as (and yes it won't work, it will just hang the browser):

async function doAsync(count){
  //external function I need to use
  count++;
  console.log("async count is "+ count );
  return await count;
}
 
function makeSyncChain(i){
  //my chain that I could change
  let isDone = false;
  let result;
  doAsync(i).then(r => {
    result = r;
  }).finally(() => {
    isDone = true;
  });
  while (!isDone);
  return result;
}

let val=0;
console.log("sync count is " + makeSyncChain(val));

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