-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 let promise} #4665
Comments
What would the code you want to be able to write look like? Is this different from the more general |
The difference is - {#await promise}
+ {#await let promise} or even like this: {#await let promise = new Promise(...)} Which is kind of, already supported. Since I can write: {#await new Promise(resolve => resolve("yay!"))}
{:then result}
{result}
{/await} I want to make What I want to write is this: {#each {length: 5} as i, index}
{#await let promise = index}
Waiting for next
{:then count}
<button on:click={() => promise = next(count)}>{count}</button>
{/await}
{/each}` the The goals:
|
without #4665, I would have to write: <script>
const promiseArray = []
const next = i => new Promise(resolve => resolve(++i))
const nextPromise = (index, i) => promiseArray[index] = next(i)
</script>
{#each {length: 5} as i, index}
{#await promiseArray[index]}
Waiting
{:then count}
<button on:click={ () => nextPromise(index, count || index) }>{count || index}</button>
{/await}
{/each} |
Closer to real world example: <script>
const fetchData = () => new Promise(resolve => resolve(/* info from server */))
const update = (info) => new Promise((resolve, reject) => {
resolve(/* updated info from server */ info)
// reject({info, errorMessage: "Could not update"})
})
</script>
{#await fetchData()}
Fetching data from server...
{:then dataSet}
{#each dataSet as data}
{#await let status} // this is the let...
Updating data...
{:then info}
<button on:click={ event => status = update(info) }>{info.title}</button>
<input on:input={ event => info.newTitle = event.target.value } value={info.title}/>
{:catch error}
Could not update {error.info.title}
<button on:click={ () => status = update(error.info) }>Retry</button>
<button on:click={ () => status = error.info }>Cancel</button>
{/await}
{/each}
{/await} |
In my opinion the svelte should keep the This proposal sure adds a lot of possibilities to the |
Ok, I can update my example to not use data fetching. But it is still relevant. |
Closing as this would introduce local state to the markup, which makes the code harder to reason about. It also doesn't seem like something like this is missed since this feature request hasn't gathered any traction. |
When iterating over a list, I would like to be able to perform actions on that list without creating a separate component with its own local variables.
repl example
with
{#await let promise}
The action is local to each iteration.and with a default value
{#await let promise = "foo"}
The text was updated successfully, but these errors were encountered: