3

Asynchronous function definitions on MongoDB (Atlas) Stitch display warnings on the GUI editor. Including the example code provided on the reference for Triggers.

The code found here can be was copied over directly to the Stitch Function editor and produces warnings because of the async keyword.

Example code from the docs.

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}.`
    });
  }
};

I want to know if Stitch supports the async/await paradigm and if I should be concerned about the warnings shown.

karloluis
  • 209
  • 2
  • 13
  • What does the warning actually say? After all its just a warning ... – Jonas Wilms May 21 '19 at 12:26
  • Most just say missing semicolon. After some trial and error I found that a semicolon after the `async` keyword made it stop showing but of course that seems to be it's linter messing up. – karloluis May 21 '19 at 16:44

1 Answers1

1

After some testing I found that at this time the async/await keywords cause the linter to throw errors and warnings. This means that for async callbacks it is best to define them separately as it will improve the linting. IE. [].map(async () => {}) will prompt errors that can be worked around.

The runtime execution returns the results as expected from standard asynchronous operations.

karloluis
  • 209
  • 2
  • 13
  • Hi, have same problem and would like to get rid of these warnings. Could you elaborate a little bit more how to fix please ? Thanks. – Eric Apr 28 '20 at 12:34
  • In short, the execution works as expected. The linter has a false issue. – karloluis Apr 29 '20 at 14:27
  • OK, true that it works despite these warnings. Nevertheless, I experienced a big difference in response times when using async/await instead of classic promises syntax (3-4 times slower). Did you ? – Eric May 03 '20 at 09:09
  • I did not encounter this, probably because we never tried pure Promises. If to speculate I suppose the Javascript runtime does better internal optimizations when using async/await syntax sugar. – karloluis May 04 '20 at 17:04
  • In fact that's the contrary, async/await much slower than promises – Eric May 05 '20 at 21:05