Skip to content

Better support for top-level await in <script setup> #4960

@mrts

Description

@mrts

What problem does this feature solve?

Currently a top-level await in a <script setup> block causes the template not to render, see this StackOverflow question. Although there are various workarounds, using top-level await is the most natural solution to the common "fetch data from the REST API and display it in the component when it loads" problem.

What does the proposed API look like?

Perhaps we can simply compile a top-level $await in a non-Suspense component to a Promise.then(), so that this snippet

<script setup>
let data = ref('')
data.value  = $await getDataFromApi()
</script>

would be compiled to

<script setup>
let data = ref('')
getDataFromApi().then(d => data.value = d)
</script>

Disclaimer: this is just a wild idea, there may be caveats that I'm unable to see at this moment and there may be better ways to achieve this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions