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

Runes: destructuring an awaited value to state variables fails #9312

Closed
PatrickG opened this issue Oct 11, 2023 · 1 comment
Closed

Runes: destructuring an awaited value to state variables fails #9312

PatrickG opened this issue Oct 11, 2023 · 1 comment

Comments

@PatrickG
Copy link
Member

Describe the bug

If a is a $state()

({ a, b } = await test());

gets compiled to

(() => {
	const tmp_1 = await test();
	($.set(a, tmp_1.a), b = tmp_1.b);
	return tmp_1;
})()

which throws this error: Cannot use keyword 'await' outside an async function (Note that you need plugins to import files that are not JavaScript) because the IIFE is not async even tho this code is in an async function.

IMO this does not need an IIFE at all. Using a block should be enough:

{
	const tmp_1 = await test();
	($.set(a, tmp_1.a), b = tmp_1.b);
}

Reproduction

REPL

Logs

No response

System Info

Svelte 5 preview

Severity

annoyance

@7nik
Copy link
Contributor

7nik commented Oct 11, 2023

It is IIFE because the expression returns the assigned value.

let c = ({ a,b } = await test());

Though, if there is a mechanism preventing variable names collision, I doubt there is need in wrapping with IIFE or a block, and tmp_1 can be nulled to prevent possible leaks.

Another solution would by pass the assigned value as IIFE's param:

((tmp_1) => {
	($.set(a, tmp_1.a), b = tmp_1.b);
	return tmp_1;
})(await test())

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
Projects
None yet
Development

No branches or pull requests

3 participants