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 arrow functions are not supported, making 'show source' unusable inside them #1208

Open
dzengi opened this issue Dec 1, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@dzengi
Copy link

dzengi commented Dec 1, 2023

I'm using the 'show source' directive, however it only works with synchronous functions. With asynchronous ones, I still only see bytecode. Android/iOS the same.

"react": "18.2.0",
"react-native": "0.72.7",
"react-native-webview": "13.6.3"

export const syncFunc = () => {
  'show source';
  let a = 5;

  for (let index = 0; index <= 10; index++) {
    a += index;
  }

  console.log('syncFunc', a);
};

export const asyncFunc = async () => {
  'show source';
  let a = 5;

  for (let index = 0; index <= 10; index++) {
    a += index;
  }

  console.log('asyncFunc', a);
};
image image
@dzengi dzengi added the bug Something isn't working label Dec 1, 2023
@avp
Copy link
Contributor

avp commented Dec 1, 2023

Thanks for the report. Hermes doesn't support async functions directly. They're being lowered by Babel to either generator functions or plain functions using regenerator by your build. In either case, the function that's being referenced by code outside the async function isn't likely to have the "show source" directive in it any more.

e.g. if your babel set up lowers to generators, you'd get this output. Notice that "show source" has been moved to an inner function.

This will be fixed when we add support for async functions directly, but until then you might be able to work around it by putting the "show source" directive in an outer non-async function.

EDIT: When I say Hermes doesn't support async functions above, I mean async arrow functions specifically.

@tmikov
Copy link
Contributor

tmikov commented Dec 2, 2023

Reclassifying as enhancement, since technically the solution to this is to implement async arrow functions.

@tmikov tmikov added enhancement New feature or request and removed bug Something isn't working labels Dec 2, 2023
@tmikov tmikov changed the title 'show source' works only with sync functions Async arrow functions are not supported, making 'show source' unusable inside them Dec 2, 2023
@KonstantinZhukovskij
Copy link

@tmikov When is a fix expected? That is, using async in regular functions (not arrows) will work?

@tmikov
Copy link
Contributor

tmikov commented Dec 2, 2023

@KonstantinZhukovskij async functions have been supported by Hermes for years. Only async arrow functions are not implemented. However I don't know how React Native configures Babel.

@dzengi
Copy link
Author

dzengi commented Dec 4, 2023

Thanks for your reply @tmikov. Could you please provide an example of how I can use the functions? Because indeed, any functions (both ordinary and arrow) turn into generators and the directive no longer works there.

@tmikov
Copy link
Contributor

tmikov commented Dec 5, 2023

Hi @dzengi, our strong recommendation is that converting a running function to source code, especially in the presence of Babel transformations, is almost never a good idea. You are much better off just copying the function into a string manually.

If you really need to do it, you need to look into how React Native configures Babel plugins in Metro. Unfortunately our team does't have much expertise in either Babel or Metro, so take this with a grain of salt and ideally confirm with someone who understands Babel and Metro:

Hermes does not currently support async arrow functions and for await. If you need these features, you need to enable Babel plugins to:

  1. Convert arrow functions to ES5 functions (probably @babel/plugin-transform-arrow-functions).
  2. Lower async generators (may be @babel/plugin-async-generator-functions).

This has not been tested by us.

The good news is that we will be releasing a beta version of Hermes supporting most ES6+ features in 2024 Q1, so at that time none of these plugins will be necessary.

Hope this helps!

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

No branches or pull requests

4 participants