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

Support async arrow function for inline event handlers #3179

Closed
lgirma opened this issue Jul 5, 2019 · 3 comments · Fixed by #3333
Closed

Support async arrow function for inline event handlers #3179

lgirma opened this issue Jul 5, 2019 · 3 comments · Fixed by #3333
Labels

Comments

@lgirma
Copy link

lgirma commented Jul 5, 2019

It would be nice to have async inline event handlers without writing the full function in the <script> tag like:

<button on:click={async () => {await services.saveData()}}>
    Save
</button>

Currently, the compiler raises the following compile error

ReferenceError: async is not defined.

REPL Demo
MDN Reference

@Conduitry Conduitry added the bug label Jul 5, 2019
@Conduitry
Copy link
Member

The compiler does not appear to be throwing an error, but the generated code is invalid, and the event handler is not labeled as async. This is a bug, but can be worked around for now by defining the click handler in the <script> block.

@Conduitry
Copy link
Member

diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts
index 3702800c..938cd843 100644
--- a/src/compiler/compile/nodes/shared/Expression.ts
+++ b/src/compiler/compile/nodes/shared/Expression.ts
@@ -363,7 +363,7 @@ export default class Expression {
 					}
 
 					const fn = deindent`
-						function ${name}(${args.join(', ')}) ${body}
+						${node.async && 'async '}function${node.generator && '*'} ${name}(${args.join(', ')}) ${body}
 					`;
 
 					if (dependencies.size === 0 && contextual_dependencies.size === 0) {

When created fully or partially hoisted function declarations, this makes sure their async-ness and generator-ness are specified correctly. I don't think there's anything else we need to do for this.

@Conduitry
Copy link
Member

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

Successfully merging a pull request may close this issue.

2 participants