AsyncDirective base class for building your own.
until
Renders one of a series of values — including Promises — in priority order. Lower-priority values are rendered as placeholders until higher-priority Promises resolve.Import
Signature
Parameters
One or more values or Promises. Values are rendered in priority order: the first argument has the highest priority. If a value is a Promise, the next lower-priority argument is rendered until the Promise resolves. Non-Promise values are rendered immediately and prevent lower-priority values from being rendered at all.
Return type
DirectiveResult — an opaque value consumed by lit-html’s template engine.
How priority works
- Arguments are indexed from
0(highest priority) ton-1(lowest priority). - The first non-Promise argument encountered wins and is rendered immediately — no lower-priority values will be shown.
- When a Promise resolves, its value replaces the current content only if it has higher priority than what is already displayed.
Example: loading placeholder
Loading... is shown immediately. Once _data resolves, it is replaced with the user’s name.
asyncAppend
Renders values from anAsyncIterable, appending each new value after the previous ones. Usable only in child (text) expressions.
Import
Signature
Parameters
An async iterable (any object with a
[Symbol.asyncIterator] method). Each value yielded by the iterable is appended to the DOM after all previously appended values.An optional mapping function applied to each yielded value before rendering. Useful for producing a
TemplateResult per item. Receives the value and its zero-based index.Return type
DirectiveResult — an opaque value consumed by lit-html’s template engine.
Example: streaming log entries
<li> without removing previous entries.
asyncReplace
Renders values from anAsyncIterable, replacing the previous value with each new one. Only the most recent value is visible at any time.
Import
Signature
Parameters
An async iterable whose values are rendered one at a time. Each new value replaces the previous rendered content.
An optional mapping function applied to each yielded value before rendering. Receives the value and its zero-based index.
Return type
DirectiveResult — an opaque value consumed by lit-html’s template engine.
Example: live clock
asyncAppend vs asyncReplace
asyncAppend | asyncReplace | |
|---|---|---|
| Behavior | Accumulates all yielded values | Shows only the latest value |
| Use case | Chat messages, log streams, infinite scroll | Live counters, status updates, tickers |
| DOM growth | Grows with each new value | Stays constant |
AsyncDirective base class
AsyncDirective is the base class for directives that need to perform asynchronous work, hold subscriptions, or react to the element being connected or disconnected from the DOM. until, asyncAppend, and asyncReplace all extend it.
Import
Properties
Read-only flag indicating whether the part this directive is bound to is currently connected to the DOM. Check this flag inside
render or update before subscribing to resources that could prevent garbage collection.Methods
setValue(value: unknown): void
Updates the directive’s part value outside of the normal render/update lifecycle. Call this from asynchronous callbacks (e.g., Promise .then(), event handlers, timers) to push a new value into the DOM.
disconnected(): void (protected)
Called when the part containing this directive is cleared or when the host element is disconnected from the DOM. Override this method to cancel subscriptions, clear timers, or release other resources.
reconnected(): void (protected)
Called when a previously disconnected part is reconnected to the DOM. Override this alongside disconnected to restore subscriptions or restart work.