listenOnce()
Returns a promise for a single event with the provided name.
listenOnce(
name: string,
signal?: AbortSignal,
): Promise<ObservableEvent<this>>;
Parameters
- name — The name of the event
- signal — An optional AbortSignal that can be used to cancel the listener (or a polyfill)
Return value
A promise that resolves with the first matching event
Description
This method adds a listener for a single event with the specified name. When the event is emitted, the listener is removed and the returned promise resolves with the event.
The promise rejects with an error with name set to AbortError if:
- The provided signal is already aborted, or becomes aborted
- The object is unlinked before the event is emitted
Examples
// Wait for a single event
let event = await someObject.listenOnce("Loaded");
// Cancel using an abort signal (e.g. in afterActive)
async afterActive(signal: AbortSignal) {
let event = await someObject.listenOnce("Loaded", signal);
// ... (not reached if deactivated before event)
}
Related
class ObservableObject
The base class of all observable objects, which can be placed into a tree structure to enable event handling and data binding.listen()
Adds a handler for all events emitted by this object.listenAsync()
Adds a handler for all events emitted by this object, and returns an async iterable.
