0

I am running in a problem with Javascript and the library ZeroRPC I created a server this way:

Server:

var zerorpc = require('zerorpc');

var server = new zerorpc.Server({
    hello: function(name, reply) {
        reply(null, 'Hello, ' + name);
    }
});

server.bind('tcp://0.0.0.0:4242');

Client

var zerorpc = require('zerorpc');

var client = new zerorpc.Client();
client.connect('tcp://127.0.0.1:4242');

var result = client.invoke('hello', 'RPC', function(error, res, more) {
    //console.log(res); //WORKS ! logs 'Hello, RPC'
    return res; //returns undefined
});

console.log(result); //undefined :(

I don't understand why I cannot return this value from the client.invoke call. Am I missing something out ?

ClemHlrdt
  • 73
  • 1
  • 7
  • 1
    The remote call is *asynchronous*. The `.invoke()` function call returns basically immediately. Later, when the RPC response is received, the platform calls your callback function. ā€“ Pointy Jun 03 '18 at 13:30
  • @Pointy thanks! Iā€™m not yet able to work with asynchronous JavaScript. How can I wait for the reply ? Promise or await ? ā€“ ClemHlrdt Jun 03 '18 at 13:38
  • Well that callback function you pass to `client.invoke()` *does* wait for the reply; anything you do inside that callback happens after the response has been received. Asynchronous programming is extremely common and idiomatic in JavaScript. ā€“ Pointy Jun 03 '18 at 13:42

0 Answers0