-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
Closed
Labels
eventsIssues and PRs related to the events subsystem / EventEmitter.Issues and PRs related to the events subsystem / EventEmitter.
Description
Continuing the discussion from the once
PR. Regarding the discussion of EventEmitter.on(emitter, "eventName")
events.on(emitter, name)
- emitter <
EventEmitter
> - name <
string
> - Returns: <
AsyncIterable
>
Creates an AsyncIterable
that yields promises for the values of the given event name
in the given EventEmitter. The iterable rejects the next promise when the EventEmitter emits 'error'. Calling .return
on the iterable removes the subscription which means the iterable unsubscribes automatically when a for await
loop is broken or returned from.
Here is an example of the API:
const events = on(emitter, "item");
events.next(); // Promise for the next event value
events.return(); // unsubscribe
events.throw(); // not sure what we want this to do, we might want to emit `error` on
Or with for await
syntax:
const foos = on(emitter, 'foo');
for await (const foo of foos) {
foo; // the next value from the emitter
break; // this calls `.return` on the async iterable and unsubscribes.
}
sindresorhus, gobengo, mikelnrd, timoxley, gfortaine and 1 moremikelnrdtunnckoCore
Metadata
Metadata
Assignees
Labels
eventsIssues and PRs related to the events subsystem / EventEmitter.Issues and PRs related to the events subsystem / EventEmitter.