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

Immediately invoked async functions lead to non-working code #5754

Closed
fhammerschmidt opened this issue Oct 25, 2022 · 4 comments
Closed

Immediately invoked async functions lead to non-working code #5754

fhammerschmidt opened this issue Oct 25, 2022 · 4 comments

Comments

@fhammerschmidt
Copy link
Member

Consider this example:

let wrapSomethingAsync: unit => unit = () => {
  let asyncFn = async () => {
    let test = await Js.Promise.resolve("Test")
    Js.log(test)
  }

  let _ = asyncFn()
}

this works and creates the following JS

function wrapSomethingAsync(param) {
  var asyncFn = async function (param) {
    var test = await Promise.resolve("Test");
    console.log(test);
  };
  asyncFn(undefined);
}

but when I immediately invoke the async function and make it anonymous

let wrapSomethingAsync: unit => unit = () => {
  let _ = (
    async () => {
      let test = await Js.Promise.resolve("Test")
      Js.log(test)
    }
  )()
}

it compiles to

function wrapSomethingAsync(param) {
  var test = await Promise.resolve("Test");
  console.log(test);
}

which misses the async keyword somewhere.

@fhammerschmidt fhammerschmidt changed the title Immediately invoked async functions lead to Immediately invoked async functions lead to non-working code Oct 25, 2022
@cristianoc
Copy link
Collaborator

@fhammerschmidt here's a simpler instance:

let willBeInlined = async () => 3

let q = willBeInlined ()

gives

async function willBeInlined(param) {
  return 3;
}

var q = 3;

Would you say this should be avoided?

@fhammerschmidt
Copy link
Member Author

I think your example is fine, as it still executes. But mine yields:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

cristianoc added a commit that referenced this issue Oct 25, 2022
@cristianoc
Copy link
Collaborator

Both are actually problematic, as async behaviour is removed from what could be long-running computations.

#5755

cristianoc added a commit that referenced this issue Oct 26, 2022
* Prevent inlining of async functions

Fixes #5754

* Update CHANGELOG.md

* Refactor logic to disable inlining of async functions.

* Prevent all async function beta reductions.
@cristianoc
Copy link
Collaborator

Will be fixed in the next 10.1 release.

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

2 participants