【问题标题】:convert this code to a version without "async - await"将此代码转换为没有“async - await”的版本
【发布时间】:2021-12-05 23:09:02
【问题描述】:

我正在尝试将此代码转换为没有“async - await”的版本。代码转换为使用(所有)承诺,没有异步和等待。但是我的控制台打印未定义时间。有人能帮我吗?它是我大学的作业。 非常感谢

(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);
  }

})();

我的代码:

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();

【问题讨论】:

  • 你为什么要在 async 函数中返回一个承诺?它会自动返回一个 Promise,所以你在 Promise 中返回一个 Promise。
  • Math.random() * 10) % 2 == 0 测试 [0,10] 范围内的随机浮点数是否是精确整数,甚至也是。这发生的概率如此之低,以至于我不认为它会在数千次或更多测试中发生。

标签: javascript async-await promise


【解决方案1】:

从你的包装函数开始 -

(async() => {

首先我们有一个正常的同步函数,这里没什么可改变的-

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

接下来我们有函数f1,它使用async关键字,但没有await任何东西。您需要做的就是删除 async 关键字 -

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

接下来我们有函数f2,它再次使用async,但不使用await。只需删除async -

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

然后我们有函数f3awaits 两个东西,rt -

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

我们可以使用.then 重写f3 -

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

你终于有了try/catch -

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

只需改用.catch -

f3().catch(exibirErro)

还有async 包装函数的结束语法。最终版本将不需要包装器。

})();

现在都在一起了-

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2022-04-02
    • 2019-08-11
    • 2019-11-08
    相关资源
    最近更新 更多