Skip to content

Support return value on EventEmitter.once #25817

@himself65

Description

@himself65

Is your feature request related to a problem? Please describe.

I'm trying to code a Promise method which will be called after once and on have been called.

and It will resolve the params that are returned from once and on functions.

I use it on server, the project code like this

module.exports.emitThen = async function emitThen (event, ...args) {
  return Promise.all(
    this.rawListeners(event).map(
      listener => Promise.resolve()
        .then(() => {
            return listener.apply(this, args)
          }
        )
    )
  )
}
const res = await obj.emitThen(eventName, data).then(res=> { 
  // do something
})

but once just returns a null because it only just be called and return nothing.

node/lib/events.js

Lines 281 to 287 in 5e1d446

function onceWrapper(...args) {
if (!this.fired) {
this.target.removeListener(this.type, this.wrapFn);
this.fired = true;
Reflect.apply(this.listener, this.target, args);
}
}

Describe the solution you'd like

function onceWrapper(...args) {
  if (!this.fired) {
    this.target.removeListener(this.type, this.wrapFn);
    this.fired = true;
    return Reflect.apply(this.listener, this.target, args);
  }
}

All in all, I think it should do the same behavior whatever on or once

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to the events subsystem / EventEmitter.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions