Event emitter -> promise

17 views
Skip to first unread message

// ravi

unread,
Jul 1, 2013, 12:41:59 PM7/1/13
to q-con...@googlegroups.com

Howdy all,

I know how to do the below in a long-winded way by creating my own wrapper promise functions around events, but I bet one of you will have a much cleverer or easier way to do it:

I have three modules that emit a “connected” event after successful connection to DBs. I want to wait for all three before I kick off my “main”. One newbie/naive way of doing this is to simply:

===========

Q.all([ db_check(db1), db_check(db2), db_check(db3) ])
.then(… call main, etc …)
.done();

function db_check(db)
{
var dfrd = Q.defer();

db.on(‘connected’, function() { dfrd.resolve(); });
db.on(‘connectfail’, function(err) { dfrd.reject(new Error(err)); });

return(dfrd.promise);
}

===========

Is there a “better” way to do this?

Thank you,

—ravi

Kris Kowal

unread,
Jul 1, 2013, 2:49:37 PM7/1/13
to Q Continuum
Without knowing more about the API you are adapting, that looks about right to me.
Reply all
Reply to author
Forward
0 new messages