New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async iterator support #3
Comments
Maybe I don't understand this correctly, but how would one unsubscribe from these events? |
By breaking out of the loop. But I don't think it's a good fit. Emitter push data, while you pull data from an Iterator. To work the emitter would have to either queue events or to skip events emitted while the async loop block is getting processed. |
@dinoboff It would queue events. That's how Emittery works already though, as events are emitted async, so they're placed on the event loop (queue). It's probably not useful for the common case, true, but sometimes I just want to gather a certain amount of events from an emitter or all, and then iterating like this would be a nice way of handling it. |
Weak references would allow the iterator to be garbage collected, and thus the queue as well. Support for that is probably still a long way off though. |
@sindresorhus they are queued to some extends but the queue get flushed on the next tick whether the consumer is ready or not. |
@bvx89 It would be something like that function iterator(emitter, eventName) {
return {
async *[Symbol.asyncIterator]() {
const queue = [];
const off = emitter.on(eventName, (data) => queue.push(data));
try {
while (true) {
if (queue.length == 0) {
await emitter.once(eventName);
}
yield queue.shift();
}
} finally {
// Called once the iterator result `return` or `throw` methods are called;
// e.g. if the loop block `break` or `throw`.
off();
}
}
}
} |
Fixed by #40 |
Could be a nicer way to handle multiple events:
Spec: https://github.com/tc39/proposal-async-iteration
The text was updated successfully, but these errors were encountered: