-
-
Save channyeintun/e986c15de03a073ff11a77027681abb1 to your computer and use it in GitHub Desktop.
Async generator for DOM events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function* eventIterator(element, eventName, subscribed) { | |
while(true) { | |
yield new Promise(resolve => { | |
const listener = e => { | |
element.removeEventListener(eventName, listener); | |
resolve(e); | |
}; | |
element.addEventListener(eventName, listener); | |
}) | |
} | |
} | |
(async () => { | |
const bodyClicks = eventIterator(document.body, 'click'); | |
for await (let event of bodyClicks) { | |
console.log(event); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment