Skip to content

Commit b898b4a

Browse files
committed
JS RPC client: await the response before returning
Wrap the consume/send exchange in a Promise so that main() truly waits for the RPC response instead of returning early.
1 parent 8a63b10 commit b898b4a

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

javascript-nodejs/src/rpc_client.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,23 @@ async function main() {
1818

1919
console.log(' [x] Requesting fib(%d)', num);
2020

21-
// Consume from the Direct Reply-to pseudo-queue (noAck is mandatory)
22-
channel.consume('amq.rabbitmq.reply-to', function(msg) {
23-
if (msg.properties.correlationId === correlationId) {
24-
console.log(' [.] Got %s', msg.content.toString());
25-
setTimeout(function() {
26-
connection.close();
27-
process.exit(0);
28-
}, 500);
29-
}
30-
}, {
31-
noAck: true
21+
const result = await new Promise((resolve) => {
22+
// Consume from the Direct Reply-to pseudo-queue (noAck is mandatory)
23+
channel.consume('amq.rabbitmq.reply-to', (msg) => {
24+
if (msg.properties.correlationId === correlationId) {
25+
resolve(msg.content.toString());
26+
}
27+
}, { noAck: true });
28+
29+
channel.sendToQueue('rpc_queue',
30+
Buffer.from(num.toString()), {
31+
correlationId: correlationId,
32+
replyTo: 'amq.rabbitmq.reply-to'
33+
});
3234
});
3335

34-
channel.sendToQueue('rpc_queue',
35-
Buffer.from(num.toString()), {
36-
correlationId: correlationId,
37-
replyTo: 'amq.rabbitmq.reply-to'
38-
});
36+
console.log(' [.] Got %s', result);
37+
await connection.close();
3938
}
4039

4140
function generateUuid() {

0 commit comments

Comments
 (0)