How To Add Promise To Event Handler In Javascript
Now I want to wrap amqp with Promise Q, here are the codes Sender.prototype.createConnection_ = function () { var deferred = Q.defer(); this.con_ = amqp.createConnection( t
Solution 1:
You're calling your functions immediately, rather than passing a function reference to the .then()
handlers. .then()
takes a function reference, not a promise as an argument. Change to this:
Sender.prototype.connect_ = function() {
returnthis.createConnection_()
.then( this.connectionReady_.bind(this) )
.then( this.createExchange_.bind(this) )
.then( this.exchangeReady_.bind(this) )
.catch( function(err) {
console.info( err );
});
}
The .bind(this)
lets you pass a function reference (something the .then()
infrastructure can call later) and still have it bound to this
.
It looks like you may also have binding issues when you pass callbacks like this:
amqp.createConnection( this.connectOpt_, this.implementOpt_ );
These callbacks will not stay bound to this
. Instead, use .bind()
like this on any callback that is method:
amqp.createConnection( this.connectOpt_.bind(this), this.implementOpt_.bind(this) );
The same issue exists several other places in your code.
Post a Comment for "How To Add Promise To Event Handler In Javascript"