Method description:
Register a single listener for the specified event, so the listener will only trigger once at most, and the listener will be released immediately after triggering.
Grammar:
emitter.once(event, listener)
Receive parameters:
event (string) Event type
listener (function) The callback function when an event is triggered
Example:
server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});
Source code:
EventEmitter.prototype.once = function(type, listener) {
if (!util.isFunction(listener))
Throw TypeError('listener must be a function');
function g() {
This.removeListener(type, g);
Listener.apply(this, arguments);
}
g.listener = listener;
this.on(type, g);
return this;
};