0

How do I have to modify the code below, so that rates: sayHello() works? If I call rates: testFunction() it works and console.log(res); in sayHello works as well. Thanks.

    exports.getRates = (req, res) => {
      res.render('riscalc/rates', {
        title: 'Riscalc',
        rates: sayHello()

      });
    };

    function testFunction() {
      return 23;
    }


    function sayHello() {
      client.invoke('s_hello', 'hello', (error, res, more) => {
        if (error) {
          console.error(error);
        } else {
                // console.log(res);
          return res;
        }
        if (!more) {
          console.log('Done.');
        }
      });
    }

1 Answers1

-1

Not sure what exactly are you trying to achieve with the code. However, assuming that client.invoke's callback is called only once, then using 'callback' function in sayHello function, the following code should work:

exports.getRates = (req, res) => {

    sayHello(function (err, val) {
        res.render('riscalc/rates', {
            title: 'Riscalc',
            rates: val
        });
    });

};

function testFunction() {
    return 23;
}


function sayHello(callback) {
    client.invoke('s_hello', 'hello', (error, res, more) => {
        if (error) {
            callback(error);
        } else {
            // console.log(res);
            callback(null, res)
        }
        if (!more) {
            console.log('Done.');
        }
    });
}
Santanu Biswas
  • 4,699
  • 2
  • 22
  • 21