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

Async await syntax breaks in compiled JS when declaring an async closure inside a loop that has access to a scoped value #6444

Closed
JonoPrest opened this issue Oct 19, 2023 · 2 comments · Fixed by #6479

Comments

@JonoPrest
Copy link
Contributor

JonoPrest commented Oct 19, 2023

There seems to be a bug that's reproduceable when you have a loop (for or while) and an async closure inside of it (defined using async await) that has access to a value that's scoped outside of the closure but inside of the loop.

I've tried it on rescript 10.1.4 and 11.0.0-rc.4 and both yield a javascript function where the await keyword is used inside of a non-async function.

//Demo.res file:
let topLevelAsyncFunction = async () => {
  for innerScopeVal in 0 to 3 {
    let asyncClosureAccessingScopedVal = async () => {
      Js.log2("Accessing scoped var inside loop", innerScopeVal)
      await Js.Promise.resolve()
    }

    await asyncClosureAccessingScopedVal()
  }
}
//Demo.bs.js compiled js file:
// Generated by ReScript, PLEASE EDIT WITH CARE


async function topLevelAsyncFunction() {
  for(var innerScopeVal = 0; innerScopeVal <= 3; ++innerScopeVal){
    var asyncClosureAccessingScopedVal = (async function(innerScopeVal){
    return function asyncClosureAccessingScopedVal() {
      console.log("Accessing scoped var inside loop", innerScopeVal);
      return await Promise.resolve(undefined);  /***<-- HERE it is using "await" keyword inside non async function ***/
    }
    }(innerScopeVal));
    await asyncClosureAccessingScopedVal(undefined);
  }
}

export {
  topLevelAsyncFunction ,
}
/* No side effect */
@cristianoc
Copy link
Collaborator

Probably one more instance of this kind: 1e0e325

@cristianoc
Copy link
Collaborator

@JonoPrest see PR #6479

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

Successfully merging a pull request may close this issue.

2 participants