Documentation

listenOnce()

Returns a promise for a single event with the provided name.

listenOnce(
name: string,
signal?: AbortSignal,
): Promise<ObservableEvent<this>>;

Parameters

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:

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