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

feat: better async transform (yield replace await) #58

Merged
merged 2 commits into from
May 3, 2024

Conversation

paoloricciuti
Copy link
Collaborator

So after today discussion i couldn't bother not trying this.

I've passed back the value from the awaited next if the function is a generator allowing for a syntax like this

task(async function*(){
    const value = yield fetch("something");
});

this still sucks in TS so we should still recommend users to do this

task(async function*(){
    const value = await fetch("something");
    yield;
});

BUT

this allow us to change the async transform to transform this

task(async() => {
    const value = await fetch("something");
});

into this

task(async function*(){
    const value = yield fetch("something");
});

the advantage of this is that if the task is aborted while the promise still hasn't resolved after it resolve we don't assign to value which could be much better if value was a reactive variable in svelte.

In this PR i've also updated the vite plugin to apply this better transform (you can see how the transform change in the generated test files)

@paoloricciuti paoloricciuti added the enhancement New feature or request label Apr 26, 2024
Copy link
Member

@nickschot nickschot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! :D

@paoloricciuti paoloricciuti merged commit b2882b6 into main May 3, 2024
4 checks passed
@paoloricciuti paoloricciuti deleted the better-async-transform branch May 3, 2024 08:50
@github-actions github-actions bot mentioned this pull request May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants