Skip to content

Instantly share code, notes, and snippets.

@DavidDurman
Created January 21, 2013 13:32
Show Gist options
  • Save DavidDurman/4586070 to your computer and use it in GitHub Desktop.
Save DavidDurman/4586070 to your computer and use it in GitHub Desktop.
var machine = _.extend({
// slots
count: 1,
done: function() {},
// logic
initialState: 'Init',
states: {
Init: {
data: {
guard: function(data) {
return --this.count == 0;
},
target: 'Done'
},
error: {
guard: function(err) {
console.log(err);
return --this.count == 0;
},
target: 'Done'
}
},
Done: {
entry: function() {
this.done();
}
}
}
}, Statechart);
function dispatch(err, doc) {
if (err)
return machine.dispatch('error', err);
machine.dispatch('data', doc);
}
machine.count = 3;
machine.done = function() {
console.log('All documents received.');
};
machine.run();
db.get('myID1', dispatch);
db.get('myID2', dispatch);
db.get('myID3', dispatch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment