Listen to one event via `addEventListener`, then resolve a Promise
npm install one-event[badge-gzip]: https://img.shields.io/bundlephobia/minzip/one-event.svg?label=gzipped
[link-bundlephobia]: https://bundlephobia.com/result?p=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.
``sh`
npm install one-event
`js`
// This module is only offered as a ES Module
import oneEvent from 'one-event';
`js
async function init() {
await oneEvent(document.body, 'click', false);
console.log('You clicked my body. Don’t do it again.');
const event = await oneEvent(document.head, ['mousedown', 'touchstart']);
console.log('Knockout with:', event.type);
}
init();
`
target 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.
#### typeOrTypes
Type: string | string[]
Matches the type parameter of addEventListener, but can also be used to listen to multiple events. The promise resolves when the first one happens.
#### 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.
- sindresorhus/p-event - Also compatible with EventEmitter so it's much bigger.
- one-mutation - Observe one mutation via MutationObserver, then resolve a Promise.querySelector
- select-dom - Lightweight /All wrapper that outputs an Array.DocumentFragment
- doma - Parse an HTML string into or one Element`, in a few bytes.
- Refined GitHub - Uses this module.
MIT © Federico Brigante