Skip to content

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

Description

@JonoPrest

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 */

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions