Await support in Meteor shell

Hi,

This would be very useful. I find myself using the shell a lot and I would really love this feature :slight_smile:

> async function x() { console.log("hi") }
> await x()
await x()
^^^^^

SyntaxError: await is only valid in async function

Thanks,
Graeme

1 Like

This isn’t specific to the Meteor shell. Top level await is disallowed by the ecmascript spec.

Top level await is supported in latest node repl through an experimental flag. Hopefully it would eventually find its way to Meteor shell too.

Usually this pattern suffices for me:

async function x() { console.log("x") }
async function y() { console.log("y") }

(async () => {
  await x();
  await y();
})()
1 Like

@gaurav7 I tried your suggestion, but it doesn’t seem to work. A better example would be this:

> async function x() { return "hello" }; (async () => { return await x(); })()
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }

How can I get the shell to print the string “hello” instead of the promise?

Meteor already wraps all shell commands in a fiber, so it would be pretty trivial to wrap them in an async too?

Try:

Promise.await((async () => {
  await x();
  await y();
})())

Edit: assuming your codebase uses https://github.com/meteor/promise

Just realized I never tried async/await in meteor shell per se, only node repl

Awesome, that works, tx