我的整个项目使用(Bluebird)Promises,但是有一个使用EventEmitter的特定库.
我希望实现以下目标:
Promise.on('connect', function() {
x.doSomething();
}).then(function() {
return new Promise(function(resolve) {
y.doAction(resolve); // this will result in `eventB` getting emitted
});
}).on('eventB', function() {
z.handleEventB();
}).then(function() {
z.doSomethingElse();
});
Run Code Online (Sandbox Code Playgroud)
我在Promises链的中间读到了EventEmitter的答案.这为我提供了一种执行'connect'事件回调的方法.这是我到目前为止的地方
var p = new Promise(function(resolve) {
emitter.on('connect', resolve);
});
p.on = function() {
emitter.on.apply(emitter, arguments);
return p;
};
p.on('connect', function() {
x.doSomething();
}).then(function() {
return new Promise(function(resolve) {
y.doAction(resolve); // this will result in eventB getting emitted
});
});
Run Code Online (Sandbox Code Playgroud)
现在如何为'eventB'进一步链接?