Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support return value on EventEmitter.once #25817

Closed
himself65 opened this issue Jan 30, 2019 · 6 comments
Closed

Support return value on EventEmitter.once #25817

himself65 opened this issue Jan 30, 2019 · 6 comments
Labels
events Issues and PRs related to the events subsystem / EventEmitter. feature request Issues that request new features to be added to Node.js.

Comments

@himself65
Copy link
Member

himself65 commented Jan 30, 2019

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

@himself65
Copy link
Member Author

himself65 commented Jan 30, 2019

Pull Request: #25818

@himself65 himself65 reopened this Feb 1, 2019
@bnoordhuis bnoordhuis added events Issues and PRs related to the events subsystem / EventEmitter. feature request Issues that request new features to be added to Node.js. labels Feb 2, 2019
@bnoordhuis
Copy link
Member

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

@himself65
Copy link
Member Author

@bnoordhuis

no

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

@simonkcleung
Copy link

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
*/

@himself65
Copy link
Member Author

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

@himself65
Copy link
Member Author

Add this support on #25818

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

3 participants