exports.emit
Mike Potra edited this page May 11, 2014
·
4 revisions
A function to be used as a replacement for EventEmitter.prototype.emit
(See emitter.emit)
Executes each of the listeners for the given event, in order.
The .emit()
used as a method will expose a wrapper
object to each of the executed listeners.
- See AsyncEmitter.prototype.emit for further details
- See AsyncEmitter.prototype.emit: Inside listener for
.next()
,.wait()
,.abort()
- The exposed
wrapper
object
emitter.emit()
returns a emitResult
, which is a wrapper around the emitter
instance, providing the additional .ready()
method.
Example #1
var asyncEmitter = require('events-async');
var emitter = new EventEmitter();
// replace the emit method
emitter.emit = asyncEmitter.emit; // preferred over `emitter.emit = asyncEmitter`
// add some listeners
emitter.on('something', function listener1() {
this.wait(); // pause the chain execution.
var wrapper = this; // just a reference for later use.
someAsyncFunction(function onAsyncFinished() {
wrapper.next(); // execute next listener in chain. (line #9)
});
});
emitter.on('something', function listener2() {
// code will execute after 'onAsyncFinished' calls .next() [line #9]
});
// Now emit something
emitter.emit('something', someParameter);
// emit again, but this time do something after the chain finished
emitter.emit('something', someOtherParameter).ready(function onready() {
if (this.ready.aborted) {
// chain was aborted
} else {
// chain finished without being aborted
}
});
Example #2
var emit = require('events-async').emit;
/**
* same as
* > emitter.emit = emit;
* > emitter.emit('something', param1, param2, ...);
*/
emit.call(emitter, 'something', param1, param2, ...);
- Home
- Installation
- Features
- Exports
- Usage (Quick examples)
- wrapper (inside listeners)
- emitResult wrapper