Turn an event stream into an async iterable
yarn add @mattkrick/asynciterify
WARNING! This is not transpiled to ES5 because the performance penalty is so high. If you need to target old browsers, include it in your webpack/rollup build!
A wrapper that turns event streams into async iterables
Because callbacks are ugly. Seriously, that's the only reason.
// from this
document.addEventListener('click', (event) => console.log('click')
// to this
import asynciterify from '@mattkrick/asynciterify'
const clicks = asynciterify(document, 'click')
for await (const event of clicks) {
console.log('click')
}
const asyncIterable = asynciterify(source, event, options)
source
: any source like a DOM element, websocket, event emitter, etc.event
: the event name that you want to listen to, e.g. 'click', 'message', 'data'options
:isEmitter
: defaults tofalse
. Iftrue
, it useson/off
methods instead ofaddEventListener, removeEventListener
(useful for EventEmitters)
MIT