Now I am learning how to write JavaScript code with promise
. Here is my case, the deliverMessage
function in Sender
try to connect with amqp. If success, then call publish_
to send messages. Otherwise, call reconnect_
to reconnect to amqp
after 3 seconds. The codes are as following,
Sender.prototype.reconnect_ = function( err ) {
console.error('MessageBus disconnected, attempting to reconnect' + err);
this.createFakeChannel_();
return setTimeout( this.deliverMessage.bind(this), 3000);
};
Sender.prototype.deliverMessage = function() {
when(amqp.connect( this.addr_ ))
.with( this )
.then( this.createChannel_ )
.then( this.createExchange_ )
.then( this.handleUnrouteableMessages_ )
.then( this.handleDisconnections_ )
.catch( this.reconnect_ )
.done( this.publish_ ); //? publish_ is invoked in all case?
};
Actually, whether the connection is succeeded or failed, publish_
is called anyway. Could anyone help me how to implement it with promise
?