add-promise-listener
Social Media Photo by Europeana on Unsplash
Using promises as generic event listener.
import addPromiseListener from 'https://esm.run/add-promise-listener';
const button = document.getElementById('test-button');
const ac = new AbortController;
addPromiseListener(
button,
'click',
{
preventDefault: true,
signal: ac.signal
}
).then(
event => {
console.log(`${event.type}ed 🥳`);
console.assert(event.currentTarget === button, 'currentTarget');
console.assert(event.defaultPrevented, 'defaultPrevented');
},
event => {
console.assert(event.currentTarget === button, 'currentTarget');
console.error(event.target.reason);
}
);
setTimeout(() => ac.abort('timeout!'), 5000);