Skip to content

Clarify behaviour when listening for 'error' event using once() #31244

Description

@aalexgabi

The documentation for once states:

Creates a Promise that is fulfilled when the EventEmitter emits the given event or that is rejected when the EventEmitter emits 'error'. The Promise will resolve with an array of all the arguments emitted to the given event.

It's not clear from the documentation what happens when once is used to listen for error event. In my case this is the desired behaviors but I was surprised when my tests were not failing because an error event was emitted.

This:

const { EventEmitter, once } = require('events');

const ee = new EventEmitter();

once(ee, 'error').then(([err]) => {
    console.log('resolves', err);
}).catch((err) => {
    console.log('rejects', err);
})

ee.emit('error', new Error('Hi'))

Prints:

resolves Error: Hi
    at Object.<anonymous> ...

Can you clarify this behaviour in the documentation please?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.eventsIssues and PRs related to EventEmitter and the events module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions