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

feature request: {#await promise then} #6270

Closed
kamholz opened this issue Apr 30, 2021 · 11 comments · Fixed by #6564
Closed

feature request: {#await promise then} #6270

kamholz opened this issue Apr 30, 2021 · 11 comments · Fixed by #6564

Comments

@kamholz
Copy link

kamholz commented Apr 30, 2021

Is your feature request related to a problem? Please describe.
If you have a promise whose resolve value you don't care about, you can currently do:

{#await promise}
  loading...
{:then}
  resolved
{/await}

but with the shorthand you can't do:

{#await promise then}
  resolved
{/await}

When you don't need to show a loading state, there is no way to omit the variable for the promise's resolve value. This is a fairly common situation, and it would be good to handle it.

Describe the solution you'd like
Support the above syntax where the variable is omitted.

Describe alternatives you've considered
You can work around this by including a variable that you don't use:

{#await promise then done}
  resolved
{/await}

But this is awkward style and may also cause issues with linters.

How important is this feature to you?
Moderate. It's not a show-stopper because there's a workaround, but it also seems like something Svelte should support, and it shouldn't be very difficult.

@furudean
Copy link

furudean commented Apr 30, 2021

A thought on the semantics of this, I think {#await promise} would make more sense than {#await promise then}. The then here doesn't have any significance in this case and could be omitted.

@kamholz
Copy link
Author

kamholz commented Apr 30, 2021

I'm pretty sure we need the then, because without it Svelte would interpret it as when the promise is pending.

@furudean
Copy link

I don't think that's the case, the compiler could check for the absence of {:then name} to determine if it's the super-shorthand or not.

@furudean
Copy link

I think this covers all the cases? https://svelte.dev/docs#await

@kamholz
Copy link
Author

kamholz commented Apr 30, 2021

I don't think that's the case, the compiler could check for the absence of {:then name} to determine if it's the super-shorthand or not.

Aside from being overly magical (in my opinion) that isn't sufficient, because then you couldn't show something during loading unless you also wanted to show something after the promise was resolved. Svelte shouldn't impose peculiar restrictions like that.

@kamholz
Copy link
Author

kamholz commented Apr 30, 2021

I think this covers all the cases? https://svelte.dev/docs#await

Yes, this is not handling any new cases per se. It's just allowing a simpler syntax for an existing case.

@furudean
Copy link

furudean commented Apr 30, 2021

Aside from being overly magical (in my opinion) that isn't sufficient, because then you couldn't show something during loading unless you also wanted to show something after the promise was resolved. Svelte shouldn't impose peculiar restrictions like that.

I'm not sure what you mean. Maybe we are misunderstanding each other? I only mean that we can omit the then in this proposed syntax because it doesn't really do anything here. It served as syntax to show which is the output in {#await promise then value}. Here it's just extra. Is that too magical?

Yes, this is not handling any new cases per se. It's just allowing a simpler syntax for an existing case.

That's not what I was referring to, I just mean from a compiler POV it isn't hard to figure out which is which either way.

@kamholz
Copy link
Author

kamholz commented Apr 30, 2021

I'm not sure what you mean. Maybe we are misunderstanding each other? I only mean that we can omit the then in this proposed syntax because it doesn't really do anything here. It served as syntax to show which is the output in {#await promise then value}. Here it's just extra. Is that too magical?

There are three promise states: pending, resolved, and rejected. {#await} can match all three. In the current syntax, this:

{#await promise}
  loading
{/await}

will match the pending state. With your proposal, there is no clean way to have an {#await} block that only matches a pending state. You'd have to do something like:

{#await promise}
  pending
{:then}
{/await}

So, while your idea does away with the superfluous syntax I'm talking about ({#await promise then done}), it introduces a new one. It also makes the syntax more confusing, because now {#await promise} would sometimes match the pending case and sometimes match the resolved case. My proposal doesn't contain superfluous syntax for any of the cases and you don't have to look beyond the immediate context to interpret what was intended. It also maintains backwards compatibility.

@furudean
Copy link

furudean commented Apr 30, 2021

Gotcha. I didn't realize that {#await promise} {/await} was valid syntax in Svelte today. Given that, I think there would have to be a keyword to differentiate it like you describe to not introduce a breaking change.

@aradalvand
Copy link

+1 The syntax is straightforward and not problematic, and there would be no breaking change or anything either. So, I'm for this.

@Conduitry
Copy link
Member

This syntax is supported now in 3.41.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants