事件触发时触发提示

时间:2015-04-29 02:04:12

标签: node.js promise bluebird

我的整个项目使用(Bluebird)Promises,但是有一个使用EventEmitter的特定库。

我希望实现以下目标:

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
    });
});

我读了EventEmitter in the middle of a chain of Promises的答案。 这为我提供了一种执行“连接”回调的方法。事件。 这是我到目前为止的地方

nodetool status <keyspace>

现在如何进一步发展事件B&#39; ?

2 个答案:

答案 0 :(得分:20)

我假设你想为每个事件做一系列不同的事情。即使eventB的操作触发connect,您也可以将其视为另一种逻辑流。

旁注:为了避免让您和其他必须阅读此代码库的人感到困惑,我建议您不要使用其他方法补充承诺,除非您非常彻底地记录它们。

从您的示例来看,似乎以下情况可行。

var Promise = require( 'bluebird' )
var emitter = someEmitter()
var connected = new Promise( function( resolve ){
    emitter.on( 'connect', resolve )
})

var eventBHappened = new Promise( function( resolve ){
    emitter.on( 'eventB', resolve )
})

connected.then( function(){
    return x.doSomething()
}).then( function(){
    return y.doSomethingElse() // will trigger `eventB` eventually
})

// this promise stream will begin once `eventB` has been triggered
eventBHappened.then( function(){ 
    return z.doSomething()
})

如果您想简化此常量

var p = new Promise( function( resolve ){
    emitter.on( 'something', resolve )
})

你可以使用这样的东西

function waitForEvent( emitter, eventType ){
    return new Promise( function( resolve ){
        emitter.on( eventType, resolve )
    })
}

将上面的代码解决方案转换为

var Promise = require( 'bluebird' )
var emitter = someEmitter()

function waitForEvent( eventEmitter, eventType ){
    return new Promise( function( resolve ){
        eventEmitter.on( eventType, resolve )
    })
}

waitForEvent( emitter, 'connect' ).then( function(){
    return x.doSomething()
}).then( function(){
    return y.doSomethingElse() // will trigger `eventB` eventually
})

// this promise stream will begin once `eventB` has been triggered
waitForEvent( emitter, 'eventB' ).then( function(){ 
    return z.doSomething()
})

因为Javascript中的函数捕获了定义它们的范围,所以此代码可以进一步简化为

var Promise = require( 'bluebird' )
var emitter = someEmitter()

function waitForEvent( type ){
    return new Promise( function( resolve ){
        //emitter has been captured from line #2
        emitter.on( type, resolve ) 
    })
}

waitForEvent( 'connect' ).then( function(){
    return x.doSomething()
}).then( function(){
    return y.doSomethingElse() // will trigger `eventB` eventually
})

// this promise stream will begin once `eventB` has been triggered
waitForEvent( 'eventB' ).then( function(){ 
    return z.doSomething()
})

答案 1 :(得分:1)

我遇到了同样的问题,并编写了一个小型的承诺包装库(controlled-promise),它允许事件发射器。您的示例的解决方案是:

const Promise = require('bluebird');
const ControlledPromise = require('controlled-promise');

const emitter = someEmitter();
const waiting = new ControlledPromise();

function waitForEvent(type) {
    return waiting.call(() => {
       emitter.once(type, event => waiting.resolve(event));
    });
}

waitForEvent('connect')
    .then(() => x.doSomething())
    .then(() => waitForEvent('eventB'))
    .then(() => z.doSomethingElse());

这种方法的好处:

  • 现有承诺在待定时自动退回
  • 轻松访问resolve() / reject()回调