[go: up one dir, main page]

one-event
TypeScript icon, indicating that this package has built-in type declarations

4.3.0 • Public • Published

one-event

Listen to one event via addEventListener, then resolve a Promise

Note: This module is based on the once option of addEventListener. Before v2 it was a replacement for it.

Install

npm install one-event
// This module is only offered as a ES Module
import oneEvent from 'one-event';

Usage

async function init() {
	await oneEvent(document.body, 'click', false);
	console.log('You clicked my body. Don’t do it again.');
}

init();

API

oneEvent(target, type, options)

target, type, and options are the same as what you supply to target.addEventListener(type, listener, options), with the addition of a filter to the options object.

options.filter

Type: (event: Event) => boolean

A filter function that will be called with the event that triggered the listener. The promise will resolve only once the filter returns true.

Alternatives

Related

  • one-mutation - Observe one mutation via MutationObserver, then resolve a Promise.
  • select-dom - Lightweight querySelector/All wrapper that outputs an Array.
  • doma - Parse an HTML string into DocumentFragment or one Element, in a few bytes.
  • Refined GitHub - Uses this module.

License

MIT © Federico Brigante

Package Sidebar

Install

npm i one-event

Weekly Downloads

862

Version

4.3.0

License

MIT

Unpacked Size

4.73 kB

Total Files

5

Last publish

Collaborators

  • fregante