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

Asynchronous default value in destructuring doesn't work #9982

Closed
ngtr6788 opened this issue Dec 22, 2023 · 0 comments
Closed

Asynchronous default value in destructuring doesn't work #9982

ngtr6788 opened this issue Dec 22, 2023 · 0 comments

Comments

@ngtr6788
Copy link
Contributor

Describe the bug

I noticed that assigning states with asynchronous expressions in variable destruction doesn't quite work.

Code:

<script>
	let name = $state("tom");
	let email = $state("tom@mail.com");
	let number = $state(555);

	const fetch_name = () => {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve("jerry")
			}, 100)
		})
	}

	const fetch_email = () => {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve("jerry@mail.com")
			}, 100)
		})
	}
	
	const fetch_number = () => {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve(777)
			}, 100)
		})
	}

	const update = async () => {
                // Code breaks here
		({ name = await fetch_name(), email = await fetch_email(), number = await fetch_number() } = {});
	}
</script>

<p>Name: {name}</p>
<p>Email: {email}</p>
<p>Number: {number}</p>
<button on:click={update}>Update me</button>

Error compiling component

Cannot use keyword 'await' outside an async function (19:53)

Possibly related: #9686

Reproduction

Here is the reproduction on the Svelte 5 preview: https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACrVTPU_DMBD9K5bF4EoVLUNVKaQRDKwVA0wEodS9CkNsR_alVRX5v2M7TZPQAZZO9r13H-_O54buRAmWJm8NVYUEmtDHqqJTiscqGHYPJYK3ra4ND0hquREVZrnKsQQkIYqsyI3FAoHlFLXM6eS-o0EWovzFPwTslo8dVS03YHrPxWIRSU9zrSySHSD__DiVYxOyykgT2BwNYG0UUXAgz0ZLYYExA1aXexi65WgBX4QEXSNjYyYkiQFe4RcYc_TSOs5Nyd18fjJdPN2lsK7RqysbTu8PiXgxvW7I11O5XC7_Pbm62vqn9nIKe1R8LIo13WoVh0IMn59Npue1GnIRC-S5y1FkBH0J54nGtYvn1aSzfqFVWmVrXyEhTSjk0lmVRfAppPZoLNHD65gzeMdLR2xqRK2IVgkvBf9eNW2fLntt-5WQzlqfzP8sqbdiJ2BLEzQ1uHf3A_Rrp36UAwAA

Logs

No response

System Info

REPL

Severity

annoyance

trueadm pushed a commit that referenced this issue Jan 9, 2024
Adds a traversion mechanism to found out if destructured expressions contain await
Fixes #9686
Fixes #9312
Fixes #9982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant