Async

A collection of asynchronous patterns for nodejs and browsers.
Made by Olivier Wietrich.

Emitter

All the projects below are based on Emitter in order to be consistent and easy to use and learn.

component/emitter

An implementation of the publish/subscribe pattern similar to the EventEmitter of nodejs.


An emitter is really great to decouple your code. It allows you to define an action and to execute it only when something interesting is happening.

var emitter = new Emitter();
emitter.on('data', function(value) {
  //do something on foo
});
emitter.emit('data', 'something');
        
Tweet

Promise

What would be asynchronous JavaScript without the promises.

bredele/promise

A clean and lightweight implementation of the famous promise pattern following the A+ spec. A promise's state can't change once resolved or rejected (transition).


A promise is a kind of proxy for a value not necessarily known at its creation time and is ideal to handle action's eventual success or failure.

var promise = new Promise();

//a promise is either resolved or rejected
promise.then(function(value) {
  //do something on resolve
}, function(reason) {
  //do something on reject
});
promise.resolve('just because');
Tweet

Mood

Give some mood to your code with a simple finite state machine.

bredele/mood

The opposite of a promise, a finite state machine has multiple states and can go from one state to an other as many times as you want.


Mood is the ideal solution whenever you need a context (ex: a widget that shows only if you are connected as admin).

var states = new Mood('normal');

//define transition open to locked
states.add('normal','connected', function() {
  //do something on transition
}, 'admin');

//trigger transition
states.emit('connected');
Tweet

Doors

You won't find this one anywhere else.

bredele/doors

A door has two states (opened and closed) and can switch from one to an other indefinitely. Doors is the opposite of a finite state machine, a transition is trigerred by one or several conditions (or locks). To be opened, a door needs to unlock all locks and one lock is enough to close the door.


For example, doors is ideal for formular validation (required fields are locks), asynchronous initialization, etc.

var door = new Doors('awesome');
door.add('lock1');
door.add('lock2');

door.on('open', function() {
  //door is opened when lock1
  //and 2 are unlocked
});

door.unlock('lock1','lock2');
Tweet

Emitter queue

A plugin to queue events.

bredele/emitter-queue

An emitter executes an action handler only when the associated event is emitted (it's actually kind of synchronous heh). Queue solves this by queuing events if there is no defined handler.


It allows you to not worry about the execution order or your application.

var emitter = new Emitter();
queue(emitter);

emitter.queue('foo');
emitter.on('foo', function() {
  //do something on foo
});
Tweet