compiler icon indicating copy to clipboard operation
compiler copied to clipboard

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

Open kireerik opened this issue 2 years ago • 4 comments

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:

  • withastro/astro#7292

  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)):

  • withastro/astro#7289

Link to Minimal Reproducible Example

Participation

  • [ ] I am willing to submit a pull request for this issue.

kireerik avatar Jun 04 '23 22:06 kireerik

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.

bluwy avatar Jun 05 '23 07:06 bluwy

Sure:

  • https://stackblitz.com/edit/github-94pv4g-ymhcxm?file=src%2Fpages%2Findex.astro

kireerik avatar Jun 05 '23 11:06 kireerik

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.

bluwy avatar Jun 05 '23 14:06 bluwy

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

natemoo-re avatar Aug 15 '23 21:08 natemoo-re