Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable toplevel await #3

Closed
Attacler opened this issue Sep 27, 2021 · 3 comments
Closed

Enable toplevel await #3

Attacler opened this issue Sep 27, 2021 · 3 comments

Comments

@Attacler
Copy link

Attacler commented Sep 27, 2021

Hi there,

Is it possible to add toplevel await?
Right now babel says the following, so im not sure if its supported:

await' is only allowed within async functions and at the top levels of modules. (1:12)

The code:

"use strict";

const start = Date.now();
process.on("exit", () => console.log('Time: ' + Date.now() - start + 'ms'));

// const esprima = require('esprima');
const babel = require("@babel/parser");
const { evaluate } = require("eval-estree-expression");
const { generate } = require("escodegen");

// const parse = (input, options) => esprima.parse(input).body[0].expression;
const parse = (input, options) => babel.parseExpression(input, options);

const context = {
  n: 6,
  foo: async function (x) {
    return (await x) * 100;
  },
  bar: async (x, y) => (await x) * 100 * y,
  obj: { x: { y: 555 } },
  z: 3,
  console,
};

const input = 'console.log(await foo(1));';
const tree = parse(input);

evaluate(tree, context, { functions: true, generate })
  .then(console.log)
  .catch(console.error);
@jonschlinkert
Copy link
Owner

Hmm, I think that might just be a babel option. I'll look into it unless someone else beats me to it.

@jonschlinkert
Copy link
Owner

I'd be open to a PR for this, I haven't had time to look into it yet.

@jonschlinkert
Copy link
Owner

I just pushed up some updates to the readme. This already worked but I didn't realize it, since an option needs to be passed to babel. Anyone interested can learn how to make this work by following this example in the readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@jonschlinkert @Attacler and others