Skip to content

Instantly share code, notes, and snippets.

@jkempff
Created July 23, 2020 07:14
Show Gist options
  • Save jkempff/3a0431e62d6215a1d6b0dbc0af71b0d4 to your computer and use it in GitHub Desktop.
Save jkempff/3a0431e62d6215a1d6b0dbc0af71b0d4 to your computer and use it in GitHub Desktop.
Async generator for DOM events
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);
}
})()
@jkempff
Copy link
Author

jkempff commented Jul 23, 2020

Not sure how to make it cancelable yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment