signals.PromiseSignal

PromiseSignal extends the Signal class to return a new promise for every dispatch call. The promise fulfill and reject methods are given to all signal listeners so that any listener can fulfill or reject the promise.

This is pretty awesome for decoupled systems that talk via signals and commands. With the promises there is now an informal approach for a command to give an answer about the result of the operation.

The code that dispatches the signal (and possibly triggers some other action somewhere in the system) often wants to know if the triggered operation was successful or not.

new signals.PromiseSignal()

The PromiseSignal constructor does not take any parameters.

Source:
Example

Creating and Using a PromiseSignal

This example shows how to setup a PromiseSignal that has one listener which fulfills the promise immediately. The fulfillment listener is invoked because it was registered as first argument to the dispatch(…).then() method of the promise.

var promiseSignal = new signals.PromiseSignal(),
    fulfillData = {},
    dispatchData = {};

function signalListener(data, promise) {

   expect.equal(data, dispatchData, 'Expected to receive dispatched data')

   // here the listener fulfills the promise!
   promise.fulfill(fulfillData);
}

// when the listener fulfills the promise this function is called
function promiseFulfilledListener(data) {
   expect.equal(data, fulfillData, 'Expected fulfill data');
}

promiseSignal.add(signalListener);
promiseSignal.dispatch(dispatchData).then(promiseFulfilledListener);

Methods

dispatch(any) → {Promise}

Dispatches the signal like the base class but returns a new promise object and provides all listeners with the promise fulfill and reject functions.

Parameters:
Name Type Description
any *

data that should be dispatched to the listeners

Source:
Returns:

A Promise A/+ instance

Type
Promise

Code Examples

Creating and Using a PromiseSignal

This example shows how to setup a PromiseSignal that has one listener which fulfills the promise immediately. The fulfillment listener is invoked because it was registered as first argument to the dispatch(…).then() method of the promise.

var promiseSignal = new signals.PromiseSignal(),
    fulfillData = {},
    dispatchData = {};

function signalListener(data, promise) {

   expect.equal(data, dispatchData, 'Expected to receive dispatched data')

   // here the listener fulfills the promise!
   promise.fulfill(fulfillData);
}

// when the listener fulfills the promise this function is called
function promiseFulfilledListener(data) {
   expect.equal(data, fulfillData, 'Expected fulfill data');
}

promiseSignal.add(signalListener);
promiseSignal.dispatch(dispatchData).then(promiseFulfilledListener);