Documentation

listenAsync()

Adds a handler for all events emitted by this object, and returns an async iterable.

listenAsync(): {
[Symbol.asyncIterator]: () => AsyncIterator<ObservableEvent>;
};

Description

This method adds a listener for all events emitted by this object, and returns an async iterable. The result can be used to handle all events using a for await...of loop. The loop body is run for each event, in the order they’re emitted, either awaiting new events or continuing execution immediately.

The loop stops when the object is unlinked, and the event handler is removed if the loop is stopped using a break statement.

Examples

// Handle all events using an async iterable
for await (let event of someObject.listenAsync()) {
  if (event.name === "Foo") {
    // ...handle Foo event
  }
}
// ... (code here runs after object is unlinked, or `break`)

Related