Await blocks

Most web applications have to deal with asynchronous data at some point. Svelte makes it easy to await the value of promises directly in your markup:

  1. {#await promise}
  2. <p>...waiting</p>
  3. {:then number}
  4. <p>The number is {number}</p>
  5. {:catch error}
  6. <p style="color: red">{error.message}</p>
  7. {/await}

Only the most recent promise is considered, meaning you don’t need to worry about race conditions.

If you know that your promise can’t reject, you can omit the catch block. You can also omit the first block if you don’t want to show anything until the promise resolves:

  1. {#await promise then value}
  2. <p>the value is {value}</p>
  3. {/await}