yoshuawuyts / barracks

:mountain_railway: action dispatcher for unidirectional data flows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to handle async data?

chiefjester opened this issue · comments

commented

Is there an example for this?

commented

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?

commented

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

commented

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.

commented

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