GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support return value on EventEmitter.once · Issue #25817 · nodejs/node

Support return value on EventEmitter.once #25817

New issue
Copy link
New issue
Copy link
Closed
Closed
Support return value on EventEmitter.once#25817
Copy link
Labels
eventsIssues and PRs related to EventEmitter and the events module.feature requestIssues requesting new Node.js features.

Description

@himself65
himself65
opened on Jan 30, 2019
Issue body actions

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

Activity

  1. himself65 commented on Jan 30, 2019

    @himself65
    himself65
    on Jan 30, 2019
    MemberAuthor
    More actions

    Pull Request: #25818

  2. himself65
    closed this as completedon Jan 31, 2019
  3. himself65
    reopened this on Feb 1, 2019
  4. bnoordhuis
    added
    eventsIssues and PRs related to EventEmitter and the events module.
    feature requestIssues requesting new Node.js features.
    on Feb 2, 2019
  5. bnoordhuis commented on Feb 2, 2019

    @bnoordhuis
    bnoordhuis
    on Feb 2, 2019
    Member
    More actions

    @himself65 You closed the PR. Does that mean you're withdrawing this feature request?

  6. himself65 commented on Feb 2, 2019

    @himself65
    himself65
    on Feb 2, 2019
    MemberAuthor
    More actions

    @bnoordhuis

    no

    just I forgot to open it, and I open it now

  7. simonkcleung commented on Feb 6, 2019

    @simonkcleung
    simonkcleung
    on Feb 6, 2019
    More actions

    Do you want to do something like this?

    const EventEmitter = require('events');
    const ev = new EventEmitter();
    
    EventEmitter.prototype.await = function(resolveEvent, rejectEvent) {
    
      var _resolve,
          _reject;
    	  
      return new Promise( (resolve, reject)=>{
        _resolve = resolve;
        this.on(resolveEvent, resolve);
        if (rejectEvent) {
          _reject = reject;
          this.on(rejectEvent, reject);
    	}
      }).finally( () => {
    	  this.off(resolveEvent, _resolve);
    	  if (rejectEvent){
    		  this.off(rejectEvent, _reject);
    	  }
    	})
    }
    
    ev.await('ready')
      .then((v) => {console.log(v); return v+2;})
      .then((v) => {console.log(v); return v+3;})
      .then((v) => {console.log(v); return v+4;})
      .then((v) => {console.log(v); return v+5;})
      .then((v) => {console.log(v)})
    ev.emit('ready', 1);
    /*
    1
    3
    6
    10
    15
    */
    Reacted by Alex Yang
  8. himself65 commented on Feb 6, 2019

    @himself65
    himself65
    on Feb 6, 2019
    MemberAuthor
    More actions

    Do you want to do something like this?

    const EventEmitter = require('events');
    const ev = new EventEmitter();
    
    EventEmitter.prototype.await = function(resolveEvent, rejectEvent) {
    
      var _resolve,
          _reject;
    	  
      return new Promise( (resolve, reject)=>{
        _resolve = resolve;
        this.on(resolveEvent, resolve);
        if (rejectEvent) {
          _reject = reject;
          this.on(rejectEvent, reject);
    	}
      }).finally( () => {
    	  this.off(resolveEvent, _resolve);
    	  if (rejectEvent){
    		  this.off(rejectEvent, _reject);
    	  }
    	})
    }
    
    ev.await('ready')
      .then((v) => {console.log(v); return v+2;})
      .then((v) => {console.log(v); return v+3;})
      .then((v) => {console.log(v); return v+4;})
      .then((v) => {console.log(v); return v+5;})
      .then((v) => {console.log(v)})
    ev.emit('ready', 1);
    /*
    1
    3
    6
    10
    15
    */

    yeah

  9. himself65 commented on Mar 5, 2019

    @himself65
    himself65
    on Mar 5, 2019
    MemberAuthor
    More actions

    Add this support on #25818

  10. himself65
    closed this as completedon Mar 5, 2019
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

    eventsIssues and PRs related to EventEmitter and the events module.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.