Skip to content

emitResult.ready

Mike Potra edited this page May 11, 2014 · 1 revision

emitResult.ready([callback])

emitResult is a wrapper around the emitter, returned by emit()


Register callback to execute after the listeners chain finished execution (aborted or not).

Each ready() is valid for the emit() call preceding it.

emitter.emit('something', someParams).ready(function onready(){
    // Executes once the chain for 'something' has finished.
})

.ready() provides several additional properties regarding the current state of the chain.

  • ready.event - The event emitted.
  • ready.running - (Boolean) true if the chain is still running, false if chain has finished.
  • ready.aborted - (Boolean) true if the chain was aborted (.running will always be false), false otherwise.
  • ready.listeners - (Number) The number of listeners in the chain.
  • ready.executed - (Number) Number of listeners executed until now. Note: This will always be >= 1, if any listeners exist in the chain

callback([wrapper])

The callback parameter provided to .ready() will receive the wrapper instance as argument.

The wrapper instance is an object that wraps around the emitter instance, providing the additional .ready

emitter.emit('something').ready(function onready(wrapper){
    // `wrapper` also provides .ready
    // this === wrapper
    
    // You can check, for example, if the chain was aborted after it finished.
    if (wrapper.ready.aborted == true) {
        // chain was aborted. It finished after `.abort()` was called inside one of the listeners.
    }
});

Example: registering two callbacks for when the chain finished.

var wrapper = emitter.emit('something');

if (wrapper.ready.running) {
    // the chain is still running.
}

wrapper.ready(function onready(){
    // do something after chain finished. [#1]
})

wrapper.ready(function onready2(){
    // do some more after chain finished. [#2]
})

Example: registering callbacks in chain.

var fn1 = function fn1() { /*...*/ };
var fn2 = function fn2() { /*...*/ };

emitter.emit('something').ready(fn1).ready(fn2);

emitResult.ready([readyEmitter])

A readyEmitter emitter can also be registered to listen for when the chain finished.

readyEmitter will receive an 'ready' event.

Event 'ready'

  • wrapper A wrapper object around the emitter whose chain finished.

Example:

var readyEmitter = new EventEmitter();

readyEmitter.on('ready', function onready(wrapper){
    // do something when `emitter` 'something' chain finished.
    if (wrapper.ready.aborted) {
        // 'something' chain on `emitter` was aborted.
    }
})

emitter.emit('something').ready(readyEmitter);