如果我有两个小fsm并希望在它们之间进行通信怎么办? 在通信过程中可能会发生诸如
之类的呼叫链 A.first(B.first(A.second(B.second)))
打破这个链我想使用promises来制作异步函数调用,如下面的代码。
function FirstTinyFSM() {
this.state = 'ready';
}
FirstTinyFSM.prototype.a = function(data) {
...
//process data
};
FirstTinyFSM.prototype.start = function() {
global_event_emiter.on('some event', this.process_some_event.bind(this));
};
FirstTinyFSM.prototype.process_some_event = function() {
Promise.resolve(1).then(function(){
second.b('some data');
})
.catch(function(err){
// handle errors
});
};
function SecondTinyFSM() {
this.state = 'ready';
}
SecondTinyFSM.prototype.b = function(data) {
...
// process data
Promise.resolve(1).then(function(){
first.a('some data');
})
.catch(function(err){
// handle errors
});
};
first = new FirstTinyFSM();
second = new SecondTinyFSM();
first.start();
这种模式有多糟糕?我还有其他选择吗?