Created
July 23, 2020 07:14
-
-
Save jkempff/3a0431e62d6215a1d6b0dbc0af71b0d4 to your computer and use it in GitHub Desktop.
Async generator for DOM events
This file contains 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
Not sure how to make it cancelable yet.