Skip to content

Commit

Permalink
suspense doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Oct 18, 2024
1 parent 218e055 commit d530f64
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/documentation/utilities/suspense.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,47 @@ layout: document
---

## Suspense Utility

The `suspense` utility allows you to lazy render content by using the [replace](../templating/index.md#replace) method in the template instances while allowing you to handle the loading and error state.

```javascript
const loadTodos = async () => {
const res = await fetch('/api/todos')

if (!res.ok) {
throw new Error(`${res.status}: ${res.statusText}`)
}

const { result } = await res.json()

return result.map((item) => html`<li>${item.name}</li>`)
}

html`<ul>
${suspense(loadTodos)}
</ul>`.render(document.body)
```

### Loading state

You can pass a custom loading content to render while the asynchronous work is being performed. The `suspense` has a default loading indicator that might not be what you want.

```javascript
html`<ul>
${suspense(loadTodos, html`<loading-spinner></loading-spinner>`)}
</ul>`.render(document.body)
```

### Error state

You can also provide a function as third argument to handle how you want to display any errors. The `suspense` has a default way that might not be what you want.

```javascript
html`<ul>
${suspense(
loadTodos,
html`<loading-spinner></loading-spinner>`,
(error) => html`<error-display error="${error}"></error-display>`
)}
</ul>`.render(document.body)
```

0 comments on commit d530f64

Please sign in to comment.