【问题标题】:Does MongoDB Stitch Functions support async/await definitions?MongoDB Stitch Functions 是否支持 async/await 定义?
【发布时间】:2019-05-22 00:47:07
【问题描述】:

MongoDB (Atlas) Stitch 上的异步函数定义在 GUI 编辑器上显示警告。包括触发器参考中提供的示例代码。

找到的代码 here 可以直接复制到缝合功能编辑器并由于 async 关键字而产生警告。

文档中的示例代码。

exports = async function (changeEvent) {
  // Destructure out fields from the change stream event object
  const { updateDescription, fullDocument } = changeEvent;

  // Check if the shippingLocation field was updated
  const updatedFields = Object.keys(updateDescription.updatedFields);
  const isNewLocation = updatedFields.some(field =>
    field.match(/shippingLocation/)
  );

  // If the location changed, text the customer the updated location.
  if (isNewLocation) {
    const { customerId, shippingLocation } = fullDocument;
    const twilio = context.services.get("myTwilioService");
    const mongodb = context.services.get("mongodb-atlas");
    const customers = mongodb.db("store").collection("customers");

    const { location } = shippingLocation.pop();
    const customer = await customers.findOne({ _id: customer_id })
    twilio.send({
      to: customer.phoneNumber,
      from: context.values.get("ourPhoneNumber"),
      body: `Your order has moved! The new location is ${location}.`
    });
  }
};

我想知道 Stitch 是否支持 async/await 范式以及我是否应该关注显示的警告。

【问题讨论】:

  • 警告实际上说的是什么?毕竟它只是一个警告......
  • 大多数只是说缺少分号。经过反复试验,我发现 async 关键字后面的分号使它停止显示,但当然这似乎是它的 linter 搞砸了。

标签: javascript mongodb async-await mongodb-stitch


【解决方案1】:

经过一番测试,我发现此时async/await关键字会导致linter抛出错误和警告。这意味着对于异步回调,最好单独定义它们,因为它会改进 linting。 IE。 [].map(async () => {}) 会提示可以解决的错误。

运行时执行返回标准异步操作所期望的结果。

【讨论】:

  • 嗨,有同样的问题,想摆脱这些警告。您能否详细说明如何解决?谢谢。
  • 简而言之,执行按预期工作。 linter 有一个错误的问题。
  • 好的,尽管有这些警告,它确实有效。尽管如此,当使用 async/await 而不是经典的 Promise 语法(慢 3-4 倍)时,我的响应时间有很大差异。你有吗?
  • 我没有遇到这种情况,可能是因为我们从未尝试过纯 Promises。如果推测一下,我认为 Javascript 运行时在使用 async/await 语法糖时会进行更好的内部优化。
  • 其实恰恰相反,async/await 比 promises 慢很多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2017-08-31
  • 2017-06-09
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多