8000 refactored sender · syk-coder/rabbitmq-tutorials@ea4d9db · GitHub
[go: up one dir, main page]

Skip to content

Commit ea4d9db

Browse files
committed
refactored sender
1 parent 7f8f3d3 commit ea4d9db

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

javascript-nodejs/src/send.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
var amqp = require('amqplib');
44
var when = require('when');
55

6-
amqp.connect('amqp://localhost').then(function(conn) {
7-
return when(conn.createChannel().then(function(ch) {
6+
var conn = amqp.connect('amqp://localhost');
7+
var ch = conn.then(createChannel).then(null, console.warn);
88

9-
var q = 'hello';
10-
var ok = ch.assertQueue(q, {durable: false});
11-
12-
return ok.then(function(_qok) {
13-
var msg = 'Hello World!';
14-
ch.sendToQueue(q, new Buffer(msg));
15-
console.log(" [x] Sent '%s'", msg);
16-
return ch.close();
9+
function createChannel(conn) {
10+
return when(
11+
conn.createChannel().
12+
then(sendMessage)).
13+
ensure(function() {
14+
conn.close();
1715
});
18-
})).ensure(function() { conn.close(); });
19-
}).then(null, console.warn);
16+
}
17+
18+
function sendMessage(ch) {
19+
var q = 'hello';
20+
var ok = ch.assertQueue(q, {durable: false});
21+
22+
return ok.then(function(_ignore) {
23+
var msg = 'Hello World!';
24+
ch.sendToQueue(q, new Buffer(msg));
25+
console.log(" [x] Sent '%s'", msg);
26+
return ch.close();
27+
});
28+
}

0 commit comments

Comments
 (0)
0