yoshuawuyts / barracks

:mountain_railway: action dispatcher for unidirectional data flows
MIT License
177 stars 22 forks source link

how to handle async data? #27

Closed chiefjester closed 9 years ago

chiefjester commented 9 years ago

Is there an example for this?

yoshuawuyts commented 9 years ago

I guess if you're not familiar with node's events it isn't straight forward. barracks is asynchronous by design.

// require barracks
const b = require('barracks')

// when 'beep' echo 'boop'
b.on('beep', () => console.log('boop'))

// wait 1 second, then call with value 'beep',
// triggering 'boop'
setTimeout(b.bind(null, 'beep'), 1000)

As displayed in the example: calling b('beep') can be done in response to any async event. Does this asnwer your question?

chiefjester commented 9 years ago

ohh, now I get it it so like pub-sub?

yoshuawuyts commented 9 years ago

Yup, exactly! Took me a while to realize that a router is actually a one-to-many relationship and an EventEmitter is the perfect abstraction for that.

chiefjester commented 9 years ago

thank you!, I'll be closing and experimenting on this.