domEventPromise.js 576 B

1234567891011121314151617
  1. //domEventPromise
  2. {
  3. let knopka = document.createElement('button')
  4. knopka.innerText='Кнопка'
  5. document.body.append(knopka)
  6. function domEventPromise(element, eventName) {
  7. function executor(resolve) {
  8. function handler(event) {
  9. element.removeEventListener(eventName, handler);
  10. resolve(event);
  11. }
  12. element.addEventListener(eventName, handler);
  13. }
  14. return new Promise(executor);
  15. }
  16. domEventPromise(knopka, 'click').then(e => console.log('event click happens', e));
  17. }