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

149
Visualizações
convert this code to a version without "async - await"

I'm trying to convert this code to a version without "async - await". Code converted to use (all) promises without async and await. But my console print undefined for time. Can someone help me? Its for a homework of my College. Thanks very much

(async() => {

  function exibirErro(err) {
    console.log(err.message);
  }

  const f1 = async(tempo) => {
    return new Promise((resolve, reject) => {
      const f = () => reject(new Error('Rejeitado'));
      console.log(`Aguardando ${tempo} segundos...`);
      setTimeout(f, tempo);
    });
  };

  const f2 = async(x) => {
    if ((Math.random() * 10) % 2 == 0) {
      throw new Error('Ops');
    }
    return Promise.resolve(1000 * x);
  };

  const f3 = async() => {
    const r = await new Promise(resolve => resolve(2));
    const t = await f2(r);
    await f1(t);
  };

  try {
    await f3();
  } catch (err) {
    exibirErro(err);
  }

})();

My code:

function exibirErro(err) {
  console.log(err.message);
}

function f1(tempo) {
  return new Promise((resolve, reject) => {
      const f = () => reject(new Error('Reject'));
      console.log(`wait ${tempo} seconds...`);
      setTimeout(f, tempo);
    })
    .then((x) => {
      if ((Math.random() * 10) % 2 == 0) {
        throw new Error('Ops');
      }
      return Promise.resolve(1000 * x);
    })
    .then((t) => {
      return Promisse.resolve(f1(t));
    }).catch((e) => {
      exibirErro(e);
    })
}

f1();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Starting with your wrapper function -

(async() => {

First we have a normal synchronous function, nothing to change here -

  function exibirErro(err) {
    console.log(err.message);
  }

Next we have function f1 which uses the async keyword but does not await anything. All you have to do here is remove the async keyword -

  const f1 = async(tempo) => {
    return new Promise((resolve, reject) => {
      const f = () => reject(new Error('Rejeitado'));
      console.log(`Aguardando ${tempo} segundos...`);
      setTimeout(f, tempo);
    });
  };

Next we have function f2, which again uses async but does not await anything. Simply remove async -

  const f2 = async(x) => {
    if ((Math.random() * 10) % 2 == 0) {
      throw new Error('Ops');
    }
    return Promise.resolve(1000 * x);
  };

Then we have function f3 that awaits two things, r and t -

  const f3 = async() => {
    const r = await new Promise(resolve => resolve(2));
    const t = await f2(r);
    await f1(t); // missing return
  };

We can rewrite f3 using .then -

  const f3 = () =>
    new Promise(resolve => resolve(2)).then(r =>
      f2(r).then(t =>
        f1(t)
      )
    )

Finally you have try/catch -

  try {
    await f3();
  } catch (err) {
    exibirErro(err);
  }

Simply use .catch instead -

f3().catch(exibirErro)

And the closing syntax for the async wrapper function. The wrapper will not be needed in the final version.

})();

Here it is all together now -

function exibirErro(err) {
  console.log(err.message);
}

const f1 = tempo => {
  return new Promise((resolve, reject) => {
    const f = () => reject(new Error('Rejeitado'));
    console.log(`Aguardando ${tempo} segundos...`);
    setTimeout(f, tempo);
  });
};

const f2 = x => {
  if ((Math.random() * 10) % 2 == 0) {
    throw new Error('Ops');
  }
  return Promise.resolve(1000 * x);
};

const f3 = () => {
  return new Promise(resolve => resolve(2)).then(r =>
    f2(r).then(t =>
      f1(t)
    )
  )
}

f3().catch(exibirErro)
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