Skip to content

Commit cd85b2d

Browse files
authored
Merge pull request amqp-node#230 from rudijs/example_send_generator
Add example send.js with generators version
2 parents b8132b2 + 47d5680 commit cd85b2d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const co = require('co');
6+
const amqp = require('amqplib');
7+
8+
co(function* () {
9+
// connection errors are handled in the co .catch handler
10+
const conn = yield amqp.connect('amqp://localhost');
11+
12+
// try catch will throw any errors from the yielding the following promises to the co .catch handler
13+
try {
14+
const q = 'hello';
15+
const msg = 'Hello World!';
16+
17+
// use a confirm channel so we can check the message is sent OK.
18+
const channel = yield conn.createConfirmChannel();
19+
20+
yield channel.assertQueue(q);
21+
22+
channel.sendToQueue(q, new Buffer(msg));
23+
24+
// if message has been nacked, this will result in an error (rejected promise);
25+
yield channel.waitForConfirms();
26+
27+
console.log(" [x] Sent '%s'", msg);
28+
29+
channel.close();
30+
}
31+
catch (e) {
32+
throw e;
33+
}
34+
finally {
35+
conn.close();
36+
}
37+
38+
}).catch(err => {
39+
console.warn('Error:', err);
40+
});

0 commit comments

Comments
 (0)