【问题标题】:From ES2018 async/await to ES2015 Promises . ... timeout从 ES2018 async/await 到 ES2015 Promises 。 ... 暂停
【发布时间】:2018-11-01 06:21:29
【问题描述】:

我正在尝试将 ES2018 async 函数转换为 ES2015 (ES6) 函数,但我得到了超时,猜想我的 ES2015 版本是错误的......但是在哪里?

ES2018版本

    async function connectGoogleAPI () {
      // Create a new JWT client using the key file downloaded from the Google Developer Console
      const client = await google.auth.getClient({
        keyFile: path.join(__dirname, 'service-key.json'),
        scopes: 'https://www.googleapis.com/auth/drive.readonly'
      });

      // Obtain a new drive client, making sure you pass along the auth client
      const drive = google.drive({
        version: 'v2',
        auth: client
      });

      // Make an authorized request to list Drive files.
      const res = await drive.files.list();
      console.log(res.data);

      return res.data;
    }

带有 Promise 的 ES2015 版本

    function connectGoogleAPI () {
      return new Promise((resolve, reject) => {
        const authClient = google.auth.getClient({
          keyFile: path.join(__dirname, 'service-key.json'),
          scopes: 'https://www.googleapis.com/auth/drive.readonly'
        });
        google.drive({
          version: 'v2',
          auth: authClient
        }), (err, response) => {
          if(err) {
            reject(err);
          } else {
            resolve(response);
          }
        }
      });
    }

【问题讨论】:

  • ... 但是Promise 是 ES6 标准功能,所以在您的 ES5 版本中您实际上使用了库?而async 是 ES7+,而不是 ES6,你的意思是 ES6 还是 ES7?
  • 您的版本错误。 :-) Promise 是 ES2015(也称为“ES6”)。 async/await 是 ES2018。
  • google.auth.getClient() 使用什么库?
  • 在您的 ES6 版本中,您是 awaiting google.auth.getClient(),但在 ES5 版本中,您没有对 google.auth.getClient() 的回调。
  • (我已经为你修复了版本。)

标签: ecmascript-6 promise async-await


【解决方案1】:

您还没有翻译getClientawait。请记住,await = then(大致)。你也成为promise creation anti-pattern 的牺牲品:当你已经有了一个承诺(来自getClient),你几乎不需要使用new Promise。只需使用then

这是一个将每个await 转换为then 的示例,使用链进行后续操作:

function connectGoogleAPI () {
  // Create a new JWT client using the key file downloaded from the Google Developer Console
  return google.auth.getClient({
    keyFile: path.join(__dirname, 'service-key.json'),
    scopes: 'https://www.googleapis.com/auth/drive.readonly'
  }).then(client => {
      // Obtain a new drive client, making sure you pass along the auth client
      const drive = google.drive({
        version: 'v2',
        auth: client
      });

      // Make an authorized request to list Drive files.
      return drive.files.list();
  }).then(res => {
      console.log(res.data);
      return res.data;
  });
}

最后一部分可以只是

  }).then(res => res.data);

...如果您删除 console.log。 (或者我们可以滥用逗号运算符。)

注意事项:

  • 每个await都需要成为then处理程序(原来有两个,等待getClientdrive.files.list
  • then 处理程序中,如果您必须等待另一个承诺(例如来自drive.files.list 的承诺),您通常从处理程序返回它,然后使用另一个处理程序来处理那个结果(这就是为什么我有return drive.files.list(),然后有一个单独的处理程序来将res转换为res.data

关于第二点:有时嵌套是合适的,例如当您需要将结果与您仅在 then 处理程序中具有的一些中间值组合时。 (例如,如果我们想将res.dataclient 结合起来。)但一般来说,最好不要嵌套。

【讨论】:

  • 非常感谢您的反馈...现在去测试一下
  • 启动并运行!太酷了……这个函数在 Firebase 函数中执行,使用默认的 Firebase 项目 App Engine 服务帐户(w 角色 Account Token Creator)。此服务帐号已获授权在给定范围内访问 Google Drive API……干得好
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2021-12-15
  • 2018-08-17
  • 2019-12-21
  • 2019-04-15
相关资源
最近更新 更多