Skip to content

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

@aalexgabi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.eventsIssues and PRs related to the events subsystem / EventEmitter.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions