🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

add-promise-listener

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
a

add-promise-listener

Using promises as generic event listener

0.1.3
latest
68

Supply Chain Security

100

Vulnerability

75

Quality

89

Maintenance

100

License

Unpopular package

Quality

This package is not very popular.

Found 1 instance in 1 package

Version published
Maintainers
1
Created
Issues
0

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',
  {
    // this is optionally needed to be sure the operation is performed
    // when it's needed and not during the next tick:
    // stopPropagation: true
    // stopImmediatePropagation: true
    preventDefault: true,
    // optional signal to eventually catch rejections
    signal: ac.signal
    // other standard options are allowed as well
    // capture: true
    // passive: true
  }
).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);
  }
);

// simulate a rejection in 5 seconds
setTimeout(() => ac.abort('timeout!'), 5000);

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts