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

Inline auto curried async functions produce invalid javascript #6007

Closed
3 of 4 tasks
lozlow opened this issue Feb 14, 2023 · 3 comments · Fixed by #6010
Closed
3 of 4 tasks

Inline auto curried async functions produce invalid javascript #6007

lozlow opened this issue Feb 14, 2023 · 3 comments · Fixed by #6010
Milestone

Comments

@lozlow
Copy link

lozlow commented Feb 14, 2023

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • [] OS and browser versions, if relevant.
  • Is it already fixed in master? Instructions

see: rescript-lang/rescript-react#89

I noticed that when passing this async function:

let onSubmit = React.useCallback(async (_a, _b) => {
    await Promise.resolve()
})

it results in the following JS code which does not compile as the function is not async:

var onSubmit = React.useCallback(function (_a) {
      return function (_b) {
        return await Promise.resolve(undefined);
      };
    });

the issue can be avoided by assigning the callback to a variable:

let cb = async (_a, _b) => {
  await Promise.resolve()
}
let onSubmit = React.useCallback(cb)

compiles to:

async function cb(_a, _b) {
  return await Promise.resolve(undefined);
}

var onSubmit = React.useCallback(function (param) {
      return function (param$1) {
        return cb(param, param$1);
      };
    });
@cristianoc
Copy link
Collaborator

This seems to have to do with @uncurry bindings and inlined functions.
Self-contained repro:

type callback<'input, 'output> = 'input => 'output

@module("react")
external useCallback: (@uncurry ('input => 'output)) => callback<'input, 'output> = "useCallback"

let onSubmit = () =>
  useCallback(async (_a, b) => {
    await b
  })

@cristianoc
Copy link
Collaborator

@lozlow this should fix the issue of the generated code: #6010

@lozlow
Copy link
Author

lozlow commented Feb 19, 2023

Thanks @cristianoc 🙌

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