Await

  1. {#await expression}...{:then name}...{:catch name}...{/await}
  1. {#await expression}...{:then name}...{/await}
  1. {#await expression then name}...{/await}

await 可以让你处理 Promise 的三种状态 pending, fulfilled, rejected

  1. {#await promise}
  2. <!-- promise is pending -->
  3. <p>waiting for the promise to resolve...</p>
  4. {:then value}
  5. <!-- promise was fulfilled -->
  6. <p>The value is {value}</p>
  7. {:catch error}
  8. <!-- promise was rejected -->
  9. <p>Something went wrong: {error.message}</p>
  10. {/await}

如果Promise状态变为reject,或者发生错误时,你不需要渲染任何东西,则catch 块可以被忽略。

  1. {#await promise}
  2. <!-- promise is pending -->
  3. <p>waiting for the promise to resolve...</p>
  4. {:then value}
  5. <!-- promise was fulfilled -->
  6. <p>The value is {value}</p>
  7. {/await}

如果你不关心pending状态,也可以省略初始化的状态。

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