withastro / compiler

The Astro compiler. Written in Go. Distributed as WASM.
Other
503 stars 59 forks source link

Waiting for tag value consistently not working in some cases in Astro components #854

Open kireerik opened 1 year ago

kireerik commented 1 year ago

What version of astro are you using?

2.5.7

Are you using an SSR adapter? If so, which one?

no

What package manager are you using?

pnpm

What operating system are you using?

Windows

What browser are you using?

Chrome

Describe the Bug

In some cases this consistently works.

The error says:

"await" can only be used inside an "async" function

or

Parse failure: Cannot use keyword 'await' outside an async function

if I try it a bit differently

  Examples:

---
---

<<any element name> set:html={await method()}/>
---
---

{await method()}
---
---

{await value}

  In this case for example it does not work:

  In this case for example it does work (consistently) (with the bug described in the (below) issue (but as I am trying the same in the context of this issue it is consistently not working)):

Link to Minimal Reproducible Example

Participation

bluwy commented 1 year ago

Can you provide a repro showing that it's not working? It seems to be working for me (stackblitz). Maybe https://live-astro-compiler.vercel.app can help you visualize how the await gets evaluated.

kireerik commented 1 year ago

Sure:

bluwy commented 1 year ago

Looks like the problem is that you can't use set:html={await ...} in a Fragment (or any component), because it generates code like:

`${$$renderComponent($$result,'Fragment',Fragment,{},{"default": () => $$render`${$$unescapeHTML(await value)}`,})}`

Maybe it could be ... "default": async () ... to fix it.

natemoo-re commented 1 year ago

Ah that's a good callout @bluwy. This is technically a compiler issue, I'm going to transfer this over there.