Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda